Example usage for java.io StreamTokenizer TT_WORD

List of usage examples for java.io StreamTokenizer TT_WORD

Introduction

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

Prototype

int TT_WORD

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

Click Source Link

Document

A constant indicating that a word token has been read.

Usage

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.j a  va2  s. co 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: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 va 2  s.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:Main.java

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

    st.parseNumbers();/* w w  w  .ja  va  2s .c  om*/
    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:MainClass.java

public static void main(String[] av) throws IOException {
    StreamTokenizer tf = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    String s = null;/*from www.j av  a  2s.  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:Counter.java

public void countWords() {
    try {// w  w  w.  java2 s.  c  o  m
        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:eu.scape_project.pt.util.ArgsParser.java

/**
 * Matches -key="value"/*from  w  w  w. jav  a  2s.  c  o  m*/
 *
 * @throws IOException
 */
private void pair() throws IOException {
    String key = null;
    String value = null;
    if (nextToken() == '-') {
        if (nextToken() == StreamTokenizer.TT_WORD && (setParameters == null || setParameters.isEmpty()
                || setParameters.contains(tokenizer.sval))) {
            key = tokenizer.sval;
        } else {
            LOG.error("unrecognized token, expecting key word");
            return;
        }
        if (nextToken() == '=') {
            LOG.debug("= found");
            if (nextToken() == '"') {
                LOG.debug("\" found");
                LOG.debug("value found");
                value = tokenizer.sval;
                mapArguments.put(key, value);
                return;
            } else
                LOG.error("unrecognized token, expecting '\"'");
        } else
            LOG.error("unrecognized token, expecting '='");
    }
    LOG.error("unrecognized token " + (tokenizer.ttype >= 32 ? (char) tokenizer.ttype : ""));

}

From source file:ScanStreamTok.java

protected void process() throws IOException {
    String s = null;//  w w w . ja 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:SimpleCalcStreamTok.java

protected void doCalc() throws IOException {
    int iType;/*from   ww w  . ja  v  a  2 s.c  om*/
    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:com.denimgroup.threadfix.framework.impl.rails.RailsModelParser.java

private void processClass(int type, String stringValue, String charValue) {
    if (type == StreamTokenizer.TT_WORD && stringValue != null) {
        modelName = stringValue;/*  w  w  w .  j a va  2  s  . c o m*/
        modelName = stringValue.replaceAll("([a-z])([A-Z]+)", "$1_$2").toLowerCase();
        currentModelState = ModelState.INIT;
    }
}

From source file:com.feilong.core.bean.ConvertUtilTest.java

/**
 * TestConvertUtilTest.//from w w  w. jav a  2 s  .com
 * 
 * @throws IOException
 */
@Test
public void testConvertUtilTest5() throws IOException {
    StreamTokenizer streamTokenizer = new StreamTokenizer(new StringReader("abaBc^babac^cb//ab/*test*/"));
    streamTokenizer.whitespaceChars('^', '^'); // Set the delimiters
    streamTokenizer.lowerCaseMode(true);

    streamTokenizer.slashSlashComments(false);
    streamTokenizer.slashStarComments(false);
    // Split comma-delimited tokens into a List
    List<String> list = new ArrayList<String>();
    while (true) {
        int ttype = streamTokenizer.nextToken();
        if ((ttype == StreamTokenizer.TT_WORD) || (ttype > 0)) {
            if (streamTokenizer.sval != null) {
                list.add(streamTokenizer.sval);
            }
        } else if (ttype == StreamTokenizer.TT_EOF) {
            break;
        }
    }

    LOGGER.debug(JsonUtil.format(list));
}