Example usage for java.io StreamTokenizer TT_NUMBER

List of usage examples for java.io StreamTokenizer TT_NUMBER

Introduction

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

Prototype

int TT_NUMBER

To view the source code for java.io StreamTokenizer TT_NUMBER.

Click Source Link

Document

A constant indicating that a number token has been read.

Usage

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;//w  ww  .  j a  v a 2s. c  o  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:MainClass.java

public static void main(String args[]) {

    try {/*from w  w w.  j a v a 2  s. c  om*/
        FileReader fr = new FileReader(args[0]);
        BufferedReader br = new BufferedReader(fr);
        StreamTokenizer st = new StreamTokenizer(br);

        st.ordinaryChar('.');

        st.wordChars('\'', '\'');

        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);
            }
        }

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

From source file:MainClass.java

public static void main(String args[]) {

    try {/*  w  w  w  .  j  ava2s.  c  om*/

        // 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);
    }
}

From source file:Main.java

public static void main(String args[]) throws IOException {
    BufferedReader inData = new BufferedReader(new InputStreamReader(System.in));
    StreamTokenizer inStream = new StreamTokenizer(inData);
    inStream.commentChar('#');
    boolean eof = false;
    do {//  w  w  w . ja  v  a  2s .c  o  m
        int token = inStream.nextToken();
        switch (token) {
        case StreamTokenizer.TT_EOF:
            System.out.println("EOF encountered.");
            eof = true;
            break;
        case StreamTokenizer.TT_EOL:
            System.out.println("EOL encountered.");
            break;
        case StreamTokenizer.TT_WORD:
            System.out.println("Word: " + inStream.sval);
            break;
        case StreamTokenizer.TT_NUMBER:
            System.out.println("Number: " + inStream.nval);
            break;
        default:
            System.out.println((char) token + " encountered.");
            if (token == '!')
                eof = true;
        }
    } while (!eof);
}

From source file:SumFile.java

static void sumfile(String filename) throws IOException {
    Reader r = new BufferedReader(new FileReader(filename));
    StreamTokenizer stok = new StreamTokenizer(r);
    stok.parseNumbers();/*from  w  w  w  . j a v a 2 s. c o  m*/
    double sum = 0;
    stok.nextToken();
    while (stok.ttype != StreamTokenizer.TT_EOF) {
        if (stok.ttype == StreamTokenizer.TT_NUMBER)
            sum += stok.nval;
        else
            System.out.println("Nonnumber: " + stok.sval);
        stok.nextToken();
    }
    System.out.println("The file sum is " + sum);
}

From source file:SumLine.java

static void sumLines(String filename) throws IOException {
    LineNumberReader lnr = new LineNumberReader(new FileReader(filename));
    lnr.setLineNumber(1);// ww  w  .ja va  2 s.  co  m
    StreamTokenizer stok = new StreamTokenizer(lnr);
    stok.parseNumbers();
    stok.eolIsSignificant(true);
    stok.nextToken();
    while (stok.ttype != StreamTokenizer.TT_EOF) {
        int lineno = lnr.getLineNumber();
        double sum = 0;
        while (stok.ttype != StreamTokenizer.TT_EOL) {
            if (stok.ttype == StreamTokenizer.TT_NUMBER)
                sum += stok.nval;
            stok.nextToken();
        }
        System.out.println("Sum of line " + lineno + " is " + sum);
        stok.nextToken();
    }
}

From source file:Counter.java

public void countWords() {
    try {//from w w  w  .  j a  v a 2s .com
        while (st.nextToken() != StreamTokenizer.TT_EOF) {
            String s;
            switch (st.ttype) {
            case StreamTokenizer.TT_EOL:
                s = new String("EOL");
                break;
            case StreamTokenizer.TT_NUMBER:
                s = Double.toString(st.nval);
                break;
            case StreamTokenizer.TT_WORD:
                s = st.sval; // Already a String
                break;
            default: // single character in ttype
                s = String.valueOf((char) st.ttype);
            }
            if (counts.containsKey(s))
                ((Counter) counts.get(s)).increment();
            else
                counts.put(s, new Counter());
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:ScanStreamTok.java

protected void process() throws IOException {
    String s = null;/* ww w . j  a v a 2  s. com*/
    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:SimpleCalcStreamTok.java

protected void doCalc() throws IOException {
    int iType;/*from w  w w.ja  v  a  2 s.  c o  m*/
    double tmp;

    while ((iType = tf.nextToken()) != StreamTokenizer.TT_EOF) {
        switch (iType) {
        case StreamTokenizer.TT_NUMBER: // Found a number, push value to
            // stack
            push(tf.nval);
            break;
        case StreamTokenizer.TT_WORD:
            // Found a variable, save its name. Not used here.
            variable = tf.sval;
            break;
        case '+':
            // + operator is commutative.
            push(pop() + pop());
            break;
        case '-':
            // - operator: order matters.
            tmp = pop();
            push(pop() - tmp);
            break;
        case '*':
            // Multiply is commutative
            push(pop() * pop());
            break;
        case '/':
            // Handle division carefully: order matters!
            tmp = pop();
            push(pop() / tmp);
            break;
        case '=':
            out.println(peek());
            break;
        default:
            out.println("What's this? iType = " + iType);
        }
    }
}

From source file:net.duckling.ddl.service.render.dml.ParseHtmlImg.java

public Map parseArgs(String argstring) throws IOException {
    HashMap<String, String> arglist = new HashMap<String, String>();

    ///*  ww  w.ja v a2  s . c  o  m*/
    //  Protection against funny users.
    //
    if (argstring == null) {
        return arglist;
    }

    StringReader in = new StringReader(argstring);
    StreamTokenizer tok = new StreamTokenizer(in);
    int type;

    String param = null;
    String value = null;

    tok.eolIsSignificant(true);

    boolean potentialEmptyLine = false;
    boolean quit = false;

    while (!quit) {
        String s;

        type = tok.nextToken();

        switch (type) {
        case StreamTokenizer.TT_EOF:
            quit = true;
            s = null;
            break;

        case StreamTokenizer.TT_WORD:
            s = tok.sval;
            potentialEmptyLine = false;
            break;

        case StreamTokenizer.TT_EOL:
            quit = potentialEmptyLine;
            potentialEmptyLine = true;
            s = null;
            break;

        case StreamTokenizer.TT_NUMBER:
            s = Integer.toString(new Double(tok.nval).intValue());
            potentialEmptyLine = false;
            break;

        case '\'':
            s = tok.sval;
            break;

        default:
            s = null;
        }

        //
        //  Assume that alternate words on the line are
        //  parameter and value, respectively.
        //
        if (s != null) {
            if (param == null) {
                param = s;
            } else {
                value = s;
                arglist.put(param, value);
                param = null;
            }
        }
    }

    //
    //  Now, we'll check the body.
    //

    if (potentialEmptyLine) {
        StringWriter out = new StringWriter();
        FileUtil.copyContents(in, out);

        String bodyContent = out.toString();

        if (bodyContent != null) {
            arglist.put(PARAM_BODY, bodyContent);
        }
    }

    return arglist;
}