Example usage for java.io StreamTokenizer parseNumbers

List of usage examples for java.io StreamTokenizer parseNumbers

Introduction

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

Prototype

public void parseNumbers() 

Source Link

Document

Specifies that numbers should be parsed by this tokenizer.

Usage

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();
    st.wordChars('_', '_');
    st.eolIsSignificant(true);//from  w ww  .j  av a 2  s.  c o  m
    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:SumFile.java

static void sumfile(String filename) throws IOException {
    Reader r = new BufferedReader(new FileReader(filename));
    StreamTokenizer stok = new StreamTokenizer(r);
    stok.parseNumbers();
    double sum = 0;
    stok.nextToken();/*from w  w  w . j  ava2  s . com*/
    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);// w w  w  . j a v a 2s . c  o  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:pipeline.misc_util.Utils.java

/**
 * Reads a text file formatted by the GSL histogram outputting functions:
 * range[0] range[1] bin[0]/*w  w w .ja va  2 s.c o m*/
 * range[1] range[2] bin[1]
 * range[2] range[3] bin[2]
 * ....
 * range[n-1] range[n] bin[n-1]
 * 
 * Writes the results as a pair of XY values into an XYSeries object from FreeChart,
 * taking the lower end of the range for each pair
 * 
 * @param fileName
 *            The path to the text file to read the data from
 * @param series
 *            The series object to write the results into
 */
public static void readHistogramIntoXYSeries(String fileName, XYSeries series) {
    final int nColumns = 3;
    // From http://www.java2s.com/Code/Java/File-Input-Output/ReadingNumbersfromaTextFile.htm
    try (Reader r = new BufferedReader(new FileReader(fileName))) {

        StreamTokenizer stok = new StreamTokenizer(r);
        stok.parseNumbers();
        int currentColumn = 0;
        double range0 = 0;
        stok.nextToken();

        while (stok.ttype != StreamTokenizer.TT_EOF) {
            if (stok.ttype == StreamTokenizer.TT_NUMBER) {
                if (currentColumn == 0) {
                    range0 = stok.nval;
                } else if (currentColumn == 2) {
                    series.add(range0, stok.nval);
                }
            } else
                Utils.log("Nonnumber while reading histogram from file " + fileName + ": " + stok.sval,
                        LogLevel.ERROR);
            stok.nextToken();
            currentColumn++;
            if (currentColumn == nColumns)
                currentColumn = 0;
        }
    } catch (FileNotFoundException e) {
        throw new RuntimeException("Could not find file " + fileName + " from which to read histogram", e);
    } catch (IOException e) {
        printStack(e);
    }

}

From source file:pipeline.misc_util.Utils.java

/**
 * Reads a text file formatted by the GSL histogram outputting functions:
 * range[0] range[1] bin[0]//w ww . java  2  s . com
 * range[1] range[2] bin[1]
 * range[2] range[3] bin[2]
 * ....
 * range[n-1] range[n] bin[n-1]
 * 
 * Writes the results as a pair of XY values into an XYSeries object from FreeChart,
 * taking the lower end of the range for each pair
 * 
 * @param plot
 *            The plot produced by a plugin
 * @param series
 *            The series object to write the results into
 */
public static void readHistogramIntoXYSeries(PluginIOCells plot, XYSeries series) {
    final int nColumns = 3;
    // From http://www.java2s.com/Code/Java/File-Input-Output/ReadingNumbersfromaTextFile.htm
    byte[] protobufArray = (byte[]) plot.getProperty("Protobuf");
    if (protobufArray == null)
        return;
    Reader r = new StringReader(new String(protobufArray));
    StreamTokenizer stok = new StreamTokenizer(r);
    stok.parseNumbers();
    int currentColumn = 0;
    double range0 = 0;
    try {
        stok.nextToken();

        while (stok.ttype != StreamTokenizer.TT_EOF) {
            if (stok.ttype == StreamTokenizer.TT_NUMBER) {
                if (currentColumn == 0) {
                    range0 = stok.nval;
                } else if (currentColumn == 2) {
                    series.add(range0, stok.nval);
                }
            } else
                Utils.log("Nonnumber while reading histogram from string " + plot.getProperty("Protobuf") + ": "
                        + stok.sval, LogLevel.ERROR);
            stok.nextToken();
            currentColumn++;
            if (currentColumn == nColumns)
                currentColumn = 0;
        }
    } catch (IOException e) {
        printStack(e);
        return;
    }

}