Example usage for java.io StreamTokenizer nextToken

List of usage examples for java.io StreamTokenizer nextToken

Introduction

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

Prototype

public int nextToken() throws IOException 

Source Link

Document

Parses the next token from the input stream of this tokenizer.

Usage

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);
    }// w  w  w . java  2 s. c o m
    r.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  ww  .j a v  a  2s . com*/
            System.out.println(st.lineno() + ") " + (char) st.ttype);
        }
    }
    fr.close();
}

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 ww  w .j  a va 2  s  .com*/
    }
    System.out.println("Number of words in file: " + wordCount);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String str = "This is a  test, 200.89  which  is  simple 50";
    StringReader sr = new StringReader(str);
    StreamTokenizer st = new StreamTokenizer(sr);
    try {//w  ww.j  a v  a2s.co  m
        while (st.nextToken() != StreamTokenizer.TT_EOF) {
            switch (st.ttype) {
            case StreamTokenizer.TT_WORD: /* a word has been read */
                System.out.println("String value: " + st.sval);
                break;
            case StreamTokenizer.TT_NUMBER: /* a number has been read */
                System.out.println("Number value:  " + st.nval);
                break;
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:ExtractTokens.java

public static void main(String[] args) throws Exception {
    FileReader fr = null;/*from w  w w. ja  v a2  s  .  c  o 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:InputOutputDemo.java

public static void main(String[] a) throws Exception {
    PrintWriter pwr = new PrintWriter(new FileWriter("java2s.txt"));
    pwr.print(4711);//from  ww w.j ava2  s.  c  om
    pwr.print(' ');
    pwr.print("Java Source and Support at www.java2s.com");
    pwr.close();

    StreamTokenizer stok = new StreamTokenizer(new FileReader("java2s.txt"));
    int tok = stok.nextToken();
    while (tok != StreamTokenizer.TT_EOF) {
        System.out.println(stok.sval);
        tok = stok.nextToken();
    }
}

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++;/*w w w.  j  a  va  2s  . com*/
        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: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 v a2  s .com*/
            System.out.println(st.lineno() + ") " + (char) st.ttype);
        }
    }
    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.eolIsSignificant(true);/*from  w w  w  . ja  v a 2 s . c om*/
    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:MainClass.java

public static void main(String args[]) {

    try {/*from  w w  w .  jav a  2  s.co m*/

        // Create a file reader
        FileReader fr = new FileReader(args[0]);

        // Create a buffered reader
        BufferedReader br = new BufferedReader(fr);

        // Create a stream tokenizer
        StreamTokenizer st = new StreamTokenizer(br);

        // Process tokens
        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:
                System.out.println(st.lineno() + ") " + (char) st.ttype);
            }
        }

        // Close file reader
        fr.close();
    } catch (Exception e) {
        System.out.println("Exception: " + e);
    }
}