Example usage for java.io StreamTokenizer StreamTokenizer

List of usage examples for java.io StreamTokenizer StreamTokenizer

Introduction

In this page you can find the example usage for java.io StreamTokenizer StreamTokenizer.

Prototype

public StreamTokenizer(Reader r) 

Source Link

Document

Create a tokenizer that parses the given character stream.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    int wordCount = 0, numberCount = 0;

    StreamTokenizer sTokenizer = new StreamTokenizer(new FileReader("yourFile.txt"));

    while (sTokenizer.nextToken() != StreamTokenizer.TT_EOF) {
        if (sTokenizer.ttype == StreamTokenizer.TT_WORD)
            wordCount++;//from   w ww.  j  av a2s  .  c  o m
        else if (sTokenizer.ttype == StreamTokenizer.TT_NUMBER)
            numberCount++;
    }

    System.out.println("Number of words in file: " + wordCount);
    System.out.println("Number of numbers in file: " + numberCount);

}

From source file:StreamTokenizerExample.java

public static void main(String[] args) throws Exception {

    Reader r = new FileReader("in.txt");
    StreamTokenizer st = new StreamTokenizer(r);
    while (st.nextToken() != StreamTokenizer.TT_EOF) {
        System.out.println(st.sval);
    }//from ww w . j  av a  2s .co m
    r.close();
}

From source file:Main.java

public static void main(String[] av) throws IOException {
    StreamTokenizer tf = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    int i;/*from   w w  w.ja  v  a 2 s .  c  o  m*/

    while ((i = tf.nextToken()) != StreamTokenizer.TT_EOF) {
        switch (i) {
        case StreamTokenizer.TT_EOF:
            System.out.println("End of file");
            break;
        case StreamTokenizer.TT_EOL:
            System.out.println("End of line");
            break;
        case StreamTokenizer.TT_NUMBER:
            System.out.println("Number " + tf.nval);
            break;
        case StreamTokenizer.TT_WORD:
            System.out.println("Word, length " + tf.sval.length() + "->" + tf.sval);
            break;
        default:
            System.out.println("What is it? i = " + i);
        }
    }

}

From source file:MainClass.java

public static void main(String[] av) throws IOException {
    StreamTokenizer tf = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    String s = null;/* ww  w  . j a  v  a 2 s . co m*/
    int i;

    while ((i = tf.nextToken()) != StreamTokenizer.TT_EOF) {
        switch (i) {
        case StreamTokenizer.TT_EOF:
            System.out.println("End of file");
            break;
        case StreamTokenizer.TT_EOL:
            System.out.println("End of line");
            break;
        case StreamTokenizer.TT_NUMBER:
            System.out.println("Number " + tf.nval);
            break;
        case StreamTokenizer.TT_WORD:
            System.out.println("Word, length " + tf.sval.length() + "->" + tf.sval);
            break;
        default:
            System.out.println("What is it? i = " + i);
        }
    }

}

From source file:ExtractTokens.java

public static void main(String[] args) throws Exception {
    FileReader fr = null;// w ww. ja  v a  2  s . co m

    fr = new FileReader(args[0]);

    StreamTokenizer st = new StreamTokenizer(fr);

    while (st.nextToken() != StreamTokenizer.TT_EOF) {
        switch (st.ttype) {
        case '"':
            System.out.println("String = " + st.sval);
            break;

        case StreamTokenizer.TT_EOL:
            System.out.println("End-of-line");
            break;

        case StreamTokenizer.TT_NUMBER:
            System.out.println("Number = " + st.nval);
            break;

        case StreamTokenizer.TT_WORD:
            System.out.println("Word = " + st.sval);
            break;

        default:
            System.out.println("Other = " + (char) st.ttype);
        }
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    StringReader reader = new StringReader("this is a test");

    int wordCount = 0;
    StreamTokenizer streamTokenizer = new StreamTokenizer(reader);

    while (streamTokenizer.nextToken() != StreamTokenizer.TT_EOF) {
        if (streamTokenizer.ttype == StreamTokenizer.TT_WORD)
            wordCount++;/*from   w  ww. jav a  2s  . c  o m*/
    }
    System.out.println("Number of words in file: " + wordCount);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    FileReader rd = new FileReader("filename.java");
    StreamTokenizer st = new StreamTokenizer(rd);

    st.parseNumbers();/* ww  w  . j av  a 2  s .co m*/
    st.wordChars('_', '_');
    st.eolIsSignificant(true);
    st.ordinaryChars(0, ' ');
    st.slashSlashComments(true);
    st.slashStarComments(true);

    int token = st.nextToken();
    while (token != StreamTokenizer.TT_EOF) {
        token = st.nextToken();
        switch (token) {
        case StreamTokenizer.TT_NUMBER:
            double num = st.nval;
            System.out.println(num);
            break;
        case StreamTokenizer.TT_WORD:
            String word = st.sval;
            System.out.println(word);
            break;
        case '"':
            String dquoteVal = st.sval;
            System.out.println(dquoteVal);
            break;
        case '\'':
            String squoteVal = st.sval;
            System.out.println(squoteVal);
            break;
        case StreamTokenizer.TT_EOL:
            break;
        case StreamTokenizer.TT_EOF:
            break;
        default:
            char ch = (char) st.ttype;
            System.out.println(ch);
            break;
        }
    }
    rd.close();
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    FileReader fr = new FileReader(args[0]);
    BufferedReader br = new BufferedReader(fr);
    StreamTokenizer st = new StreamTokenizer(br);
    st.eolIsSignificant(true);//  www .  ja  va 2 s  .  c o m
    int lines = 1;

    while (st.nextToken() != StreamTokenizer.TT_EOF) {
        switch (st.ttype) {
        case StreamTokenizer.TT_EOL:
            ++lines;
        }
    }
    System.out.println("There are " + lines + " lines");
    fr.close();
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    FileReader fr = new FileReader(args[0]);
    BufferedReader br = new BufferedReader(fr);
    StreamTokenizer st = new StreamTokenizer(br);
    st.whitespaceChars(',', ',');
    while (st.nextToken() != StreamTokenizer.TT_EOF) {
        switch (st.ttype) {
        case StreamTokenizer.TT_WORD:
            System.out.println(st.lineno() + ") " + st.sval);
            break;
        case StreamTokenizer.TT_NUMBER:
            System.out.println(st.lineno() + ") " + st.nval);
            break;
        default:/*from   w w w .j a va 2 s.c  om*/
            System.out.println(st.lineno() + ") " + (char) st.ttype);
        }
    }
    fr.close();
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    FileReader fr = new FileReader(args[0]);
    BufferedReader br = new BufferedReader(fr);
    StreamTokenizer st = new StreamTokenizer(br);
    while (st.nextToken() != StreamTokenizer.TT_EOF) {
        switch (st.ttype) {
        case StreamTokenizer.TT_WORD:
            System.out.println(st.lineno() + ") " + st.sval);
            break;
        case StreamTokenizer.TT_NUMBER:
            System.out.println(st.lineno() + ") " + st.nval);
            break;
        default://from  w  w w  .  java2  s. c om
            System.out.println(st.lineno() + ") " + (char) st.ttype);
        }
    }
    fr.close();
}