Using a BufferedReaderobject in a StreamTokenizer

I can't seem to figure out how to use the BufferedReader object I have created (called input) as the source for a new StreamTokenizer.
I do this
StreamTokenizer adder = new StreamTokenizer(System.setIn(input));
and it says variable input not found in class yet I have created before thus:
try {
FileReader r = new FileReader("c:\\JavaProjects\\TMA01\\Streams\\MyInputFile.txt");
BufferedReader input = new BufferedReader(r);
catch (FileNotFoundException e) {System.exit(0);}
Any ideas anyone.

I think the problem is simply thay 'input' only hac scope within the try block.
This removes the error:
BufferedReader input;
try
FileReader r = new FileReader("c:\\JavaProjects\\TMA01\\Streams\\MyInputFile.txt");
input = new BufferedReader(r);
catch (FileNotFoundException e)
System.exit(0);
however,
StreamTokenizer adder = new StreamTokenizer(System.setIn(input));
doesn't work, as input is not an input stream. I would use FileInputStream

Similar Messages

  • Can i link a StreamTokenizer with a String Tokenizer?

    As recently informed, I now know that I can treat delimiters as tokens using StringTokenizer. My next question, is how do I use a StringTokenizer with a StreamTokenizer to let me read a word (eg cat) as a token and also a "," as a token? The reason being that I would like to be able to use the 'end of file' and 'end of line' flags in the StreamTokenizer but also would like to treat certain delimiters as tokens using the StringTokenizer!
    If this is not possible, would I need to use the StringTokenizer and treat a space(" ") as a token (or delimiter)?
    If I read a token with the StringTokenizer can I expect the StreamTokenizer to read the next token after that, or is that wishfull thinking?
    The following code was used just to read words as tokens. I would like to incorporate the StringTokenizer to set delimiters as tokens to be read. How??????
    try
        File file = new File("test.txt");
        FileInputStream stream = new FileInputStream (file);
        InputStreamReader reader = new InputStreamReader (stream);
        BufferedReader buffer = new BufferedReader (reader);
        StreamTokenizer tokenize = new StreamTokenizer (buffer);
        tokenize.nextToken();
        while (tokenize.ttype != StreamTokenizer.TT_EOF)
            data = tokenize.sval;
            tokenize.nextToken();
    catch(IOException e)
        System.out.println("Error in file input:" + e.toString());
        }

    I don't think you should do it that way, it can get a little nasty. Just use a BufferedReader to read one line in at a time, then use a StringTokenizer to break it up. When a reader has reached the eof, the readLine returns a null.

  • Reading a file line by line from right to left using StringTokenizer

    I hope the title caught you attention!!!
    I am a little stuck. How can I implement a method that will allow me to read a sentence from right to left when reading from file.
    Ex.
    IX: Series      {2}
    IX: {Series!geometric|(}      {4}
    IX: {Euler's constant}      {4}
    IX: {Series!geometric|)}      {4}
    IX: {Series!arithmetic|(}      {4}
    IX: {Series!arithmetic|)}      {5}
    IX: {Series!harmonic|(}      {5}
    IX: {Euler's constant}      {5}
    IX: {Series!harmonic|)}      {5}
    IX: Series      {5}
    So for example in the first row we have
    IX: Series      {2}
    and what the output should show be is
    Series: 2
    What happens in the background is that I created an entry for a TreeMap which will be display at the end.
    Thanks for your time and appreciate your help kindly.

    Well, as a left-to-right start:
    public class TopicInputReader extends BufferedReader {
        public static final int STARTTOPIC = 0;
        public static final int ENDTOPIC   = 1;
        public static final int SUBTOPIC   = 2; 
        // Instead of making Topic members public, you should make accessors.
        // Didn't feel like typing.
        public class Topic {
            public int    type; // STARTTOPIC, ENDTOPIC, or SUBTOPIC
            public String name;
            public String parent;
            public int    page;
        public TopicInputReader (Reader in) {
            super(in);
        public TopicInputReader (Reader in, int sz) {
            super(in, sz);
        public Topic readTopic () throws IOEXception {
            String          line;
            Topic           topic;
            StringTokenizer tokens;
            String          token;
            line = readLine();
            if (line = null) throw new EOFException();
            tokens = new StringTokenizer(line);
            topic.parent = "".intern();
            // Note: If StringTokenizer isn't appropriate for you, you
            // can use a StringReader in conjunction with a StreamTokenizer.
            // Ex: token = new StreamTokenizer(new StringReader(line));
            // TODO: Set up tokenizer parameters here.
            while (tokens.hasMoreTokens()) {
                token = tokens.nextToken(appropriate_delimiter);
                // TODO: Fill in 'topic' fields here.
                // Keep in mind, since you're reading left-to-right and you don't
                // know if a particular string is going to be a topic name or a
                // sub topic, you can always just read a topic name into topic.name
                // and, when you encounter a new topic name:
                //    topic.parent += ('!' + topic.name);
                //    topic.name = token;
            return topic;
    };I definitely have not tested that... or even double-checked it.
    Although... the format of your topic list file does lend itself well to right-to-left parsing. Since you are insisting, I suggest reversing the string and using a StringTokenizer (or a StreamTokenizer and a StringReader):
        BufferedReader  r = new BufferedReader(new InputStreamReader(myInputStream));
        String          line, revline;
        StreamTokenizer tokens;
        while (...) {
            line    = r.readLine();
            revline = (new StringBuffer(line)).reverse().toString();
            tokens  = new StreamTokenizer(new StringReader(line));
            // Parse tokens here.
        };Jason

  • Read method calls from a file.

    I am working on an event driven program that has various events listed in a file that are supposed to be read in and used. I can go through the pain staking process of using StringTokenizer and .substring to figure which event is which, but I want to know if there would be an easier process of reading in the events. (Can't Cast them)
    Any suggestions?
    Shawn

    Hi Again
    U can go for StreamTokenizer in java.io
    this is useful for tokenizing a Stream based on delimiters
    It has Two Cons for both Type of Streams either Char or Bin
    public java.io.StreamTokenizer(java.io.InputStream);
    public java.io.StreamTokenizer(java.io.Reader);
    The StreamTokenizer class takes an input stream and parses it into "tokens", allowing the tokens to be read one at a time. The parsing process is controlled by a table and a number of flags that can be set to various states. The stream tokenizer can recognize identifiers, numbers, quoted strings, and various comment styles.
    U hav to invoke a method called nextToken Form which u will get a token which can be compared to find wether it is a word or character ....using the constanrs avlbl in StreamTokenizer class
    TT_WORD indicates that the token is a word.
    TT_NUMBER indicates that the token is a number.
    TT_EOL indicates that the end of line has been read. The field can only have this value if the eolIsSignificant method has been called with the argument true.
    TT_EOF indicates that the end of the input stream has been reached
    Alll the Very Best
    Rgds
    Sriram

  • What alternative wuld there be to using StreamTokenizer?

    Hello,
    Currently I am using a StreamTokenizer to parse data from a file. The problem is that it goes way too slow. I'd like a faster way to process the data. If there is a better alternative to using StreamTokenizer I'd like to know. I've looked around and haven't found anything. I though using the java.nio stuff might help but now the I/O is faster in comparison to the BufferedReader, but the StreamTokenizer is keeping the I/O capabilities slow. The file sizes range from 9M - 20M and I need to find values and hold the data. I listed a very generic structure for the files I'm reading in below. I use the Headings(ex: [Values]) to sort through the data under the heading and then move on using the token.nextToken. If anyone could give me some ideas I'd appreciate it. Thank you in advance.
    An Example:
    [Values]
    1 = "String1"
    2 = "String2"
    [Data Part1]
    JOB_1 = 1021201212
    PART_1 = 21231331
    [TESTS]
    T_000_= Data; Data;Data
    through
    T_1442 = Data; Data; Data

    Currently I am using a StreamTokenizer to parsedata
    from a file.Write a custom parser.
    And if you parsing the same data more than once then
    put the data into another format on the first parse so
    it is faster on the subsequent tries.I think this would be your best bet right now. Because public static void mySimpleTokenizer()String s, String delimiter)
       String sub = null;
       int i =0;
       int j =s.indexOf(delimiter);  // First substring
       while( j >= 0) {
       sub = s.substring(i,j);
         i = j + 1;
         j = s.indexOf(delimiter, i);   // Rest of substrings
       sub = s.substring(i); // Last substring
    }I read some where that the above method works almost 4 times faster than StringTokenizer, because of less overhead.
    Also just wondering if the following links could be of any help
    http://ostermiller.org/utils/StringTokenizer.html
    http://www.javaperformancetuning.com/news/roundup032.shtml
    http://www.ftponline.com/javapro/2002_08/online/servletsjsp_08_06_02/Java%20Servlets%20Ch16.pdf

  • ToString and StreamTokenizer - general use questions

    Hello everyone.
    I am currently taking a class in Java at a NYC school, and got into an argument with the teacher regarding the proper use of the toString method and the SteamTokenizer class. He claims the following:
    1. toString() methods are primarily for debugging - i.e., I defined this method in a class I created in order to better represent the class' state visually - and he marked that as wrong - since I was not using the method 'properly'.
    2. I was using the StreamTokenizer class in order to read in user input from the terminal - and only perform something if values entered were numbers. He claims that my use of the class is inappropriate - since it "doesn't do what I think it does" - whatever that means.
    I just wanted to see what people's feelings were on the proper use of the toString method and the StreamTokenizer class (in regards to reading user input). Thanks!

    also on your last remark,
    if the example you use the toString-method insteadof
    the getAccountNumber-method: how is a developer to
    know what the toString-method will give; that'swhat
    I mean with vague. 'getAccountNumber' does notleave
    much roam for imagination.Yes, but if you give the developer an API for your
    program (if he can't see the code himself), the API
    will (should!) tell him what toString will return.Maybe you work in a project where developers actually have time to do this. I agree that they should and that the API should always be up-to-date but I've never encountered a project where this is actually done.
    Actually, I don't really understand your problem with this statement. Don't you agree that a method getAccountNumber should be created and that this should not only be returned by the toString-method. If you want your toString to return this value you can allways do this, for debugging purposes.
    Another important argument; in these cases you do not excpect the toString to return important information that is called from other methods. What if another developer needs to debug and changes the debug-information? I know that where I work it's the first developer who would get blamed for this.

  • How to use StreamTokenizer class ?

    i want to use StreamTokenizer class in my program so that when i read from a file i want to make tokens of words present in the file, but i dont know how to use this class in my program, as i didnt find any example which shows how to use StreamTokenizer class. Can nebody knows how to use it in a program ?

    Hi,
    here's some sample code of how to use the StreamTokenizer.
    in = new StreamTokenizer(
    new BufferedReader(
    new FileReader(fname)));
    You can iterate through the file with:
    while(in.nextToken() != StreamTokenizer.TT_EOF)
    when you call nextToken() ttype is filled with the type of token, you can't test ttype for what kind of token,
    test for word:
    if (in.ttype == StreamTokenizer.TT_WORD)
    test for word:
    if (in.ttype == StreamTokenizer.TT_WORD)
    I think you can grasp the rest in javadoc

  • How to use StreamTokenizer

    hello
    i have a file like that :
    02-20-04 11:02AM 1307472 liste7.pdf
    02-20-04 11:02AM 1807972 liste2.pdf
    02-20-04 11:02AM 1557972 liste1.pdf
    i want to use StreamTokenizer to get the number part 1307472 ,1807972 , 1557972
    knowing that they bigin all at coolumn 31 and finich at column 38
    then i want to convet it into an int
    and to add them
    hellllllllp

    do you know how i can convert a setring to an integerThe Integer class has a parseInt method. You must enclose it in a try block and catch the NumberFormatException it can throw if the String isn't a valid int.

  • Read text file using Java(streamTokenizer)

    Hi, all,
    I am lost when trying to read data from a text file to a Java prgram. The text file looks like the following:
    106,62,2322,8159,1
    106,62,3658,8333,1
    106,62,4215,8334,2
    Each number is seperated by "," and each line representing one row of data. I was thinking about using streamTokenizer to read the data into a multi-dimentional array. Since I am new to Java and just read something about the streamTokenizer from book, I would like to get some help from someone who is more experienced with that.
    Thanks for your help!
    Kevin

    Hi Kevin,
    try this:
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.StringTokenizer;
    public class Answer {
            public static void main(String[] args) {
            List data = new ArrayList();
            try {
                BufferedReader in = new BufferedReader(new FileReader("...your text file ..."));
                String line;
                // reading the file line by line
                while ((line = in.readLine()) != null) {
                    // splitting the line into token
                    StringTokenizer st = new StringTokenizer(line, ",");
                    List row = new ArrayList();
                    while (st.hasMoreTokens()) {
                        row.add(new Integer(st.nextToken()));
                    data.add(row); // adding the row of data
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            // test result
            System.out.println(data);
    }I don't like to use arrays, because when I start reading the file, I don't know yet, how many rows of data it is containing. Therefore a java.util.List is much more convenient (you don't have to initialize). Your result is now a java.util.List containing elements of java.util.List containig elements of Integer.
    Harri

  • How can I use streamtokenizer to parse out metatag?

    everything is in the title I want to identify the author or the title of anURL how can I do?

    Hi!
    May be it's better to use regular expressions.
    They are available in jdk1.4 or you can use gnu-regexp-1.1.4.jar

  • How to compare content of two text file using StreamTokenizer

    hi....
    i have two text files...containg field like(name,number,scheme) and(number,date,value)... i want to create a third file containg field like (name,number,date,scheme,value) by using these two table. how to create

    I think this code can solve your problem.
    private static final String DELIM = ",";
    * Compile two files.
    * @param file1 String the input file 1
    * @param file2 String the input file 2
    * @param file3 String the output file
    * @throws IOException error in reading/writing
    public void compileFiles(String file1, String file2, String file3) throws
            IOException {
            BufferedReader reader1 = new BufferedReader(new InputStreamReader(
                new FileInputStream(file1)));
            BufferedReader reader2 = new BufferedReader(new InputStreamReader(
                new FileInputStream(file2)));
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(file3)));
            String line1 = reader1.readLine();
            String line2 = reader2.readLine();
            while (line1 != null && line2 != null) {
                writer.write(compileLines(line1, line2));
                writer.newLine();
                line1 = reader1.readLine();
                line2 = reader2.readLine();
            reader1.close();
            reader2.close();
            writer.close();
        private String compileLines(String line1, String line2) {
            StringTokenizer tok1 = new StringTokenizer(line1, DELIM);
            StringTokenizer tok2 = new StringTokenizer(line2, DELIM);
            String name = tok1.nextToken();
            String number = tok1.nextToken();
            String scheme = tok1.nextToken();
            // ignore number
            tok2.nextToken();
            String date = tok2.nextToken();
            String value = tok2.nextToken();
            StringBuffer buffer = new StringBuffer();
            buffer.append(name);
            buffer.append(DELIM);
            buffer.append(number);
            buffer.append(DELIM);
            buffer.append(date);
            buffer.append(DELIM);
            buffer.append(scheme);
            buffer.append(DELIM);
            buffer.append(value);
            return buffer.toString();
        }

  • How to retrieve IndividualStrings from a txt file using String Tokenizer.

    hello can any one help me to retrieve the individual strings from a txt file using string tokenizer or some thing like that.
    the data in my txt file looks like this way.
    Data1;
    abc; cder; efu; frg;
    abc1; cder2; efu3; frg4;
    Data2
    sdfabc; sdfcder; hvhefu; fgfrg;
    uhfhabc; gffjcder; yugefu; hhfufrg;
    Data3
    val1; val2; val3; val4; val5; val6;
    val1; val2; val3; val4; val5; val6;
    val1; val2; val3; val4; val5; val6;
    val1; val2; val3; val4; val5; val6;
    i need to read the data as an individual strings and i need to pass those values to diffarent labels,the dat in Data3 i have to read those values and add to an table datamodel as 6 columns and rows depends on the data.
    i try to retrieve data using buffered reader and inputstream reader,but only the way i am retrieving data as an big string of entire line ,i tried with stringtokenizer but some how i was failed to retrive the data in a way i want,any help would be appreciated.
    Regards,

    Hmmm... looks like the file format isn't even very consistent... why the semicolon after Data1 but not after Data2 or Data3??
    Your algorithm is reading character-by-character, and most of the time it's easier to let a StringTokenizer or StreamTokenizer do the work of lexical analysis and let you focus on the parsing.
    I am also going to assume your format is very rigid. E.g. section Data1 will ALWAYS come before section Data2, which will come before section Data3, etc... and you might even make the assumption there can never be a Data4, 5, 6, etc... (this is why its nice to have some exact specification, like a grammar, so you know exactly what is and is not allowed.) I will also assume that the section names will always be the same, namely "DataX" where X is a decimal digit.
    I tend to like to use StreamTokenizer for this sort of thing, but the additional power and flexibility it gives comes at the price of a steeper learning curve (and it's a little buggy too). So I will ignore this class and focus on StringTokenizer.
    I would suggest something like this general framework:
    //make a BufferedReader up here...
    do
      String line = myBufferedReader.readLine();
      if (line!=null && line.trim().length()>0)
        line = line.trim();
        //do some processing on the line
    while (line!=null);So what processing to do inside the if statement?
    Well, you can recognize the DataX lines easily enough - just do something like a line.startsWith("Data") and check that the last char is a digit... you can even ignore the digit if you know the sections come in a certain order (simplifying assumptions can simplify the code).
    Once you figure out which section you're in, you can parse the succeeding lines appropriately. You might instantiate a StringTokenizer, i.e. StringTokenizer strtok = new StringTokenizer(line, ";, "); and then read out the tokens into some Collection, based on the section #. E.g.
    strtok = new StringTokenizer(line, ";, ");
    if (sectionNo==0)
      //read the tokens into the Labels1 collection
    else if (sectionNo==1)
      //read the tokens into the Labels2 collection
    else //sectionNo must be 2
      //create a new line in your table model and populate it with the token values...
    }I don't think the delimiters are necessary if you are using end-of-line's as delimiters (which is implicit in the fact that you are reading the text out line-by-line). So the original file format you listed looks fine (except you might want to get rid of that rogue semicolon).
    Good luck.

  • How can i read a string with nextToken() of StreamTokenizer

    I need for my class paper to read a string from a file and i used the StreamTokenizer's method nextToken but i can not read a string with it. my code is:
    StreamTokenizer st = new StreamTokenizer(the code that gets the input from the file)
    String line;
    while ((line !=br.readLine()) != null) {
    String surname = (st.nextToken()).trim();
    but it gets me some error of:
    int can not be dereferenced
    what should I do to get the string i need?

    Look at the API for java.io.StreamTokenizer. In particular, look at the return type for nextToken().
    Good luck.

  • StreamTokenizer NVAL bug? 16 digits not reading correctly?

    hi all!
    i do some calculations which are highly precise, and we use numbers with 16 digits after the comma. this works most of the time without problems. but for some numbers java rounds it automatically. i think the problem is within the StreamTokenizer nval method.
    but please see the code to get what i mean:
    private static void readFromFileTestNumberProblem() {
              try{               
                   FileReader reader = new FileReader(new File("test.txt"));
                   StreamTokenizer tokenStream = new StreamTokenizer(reader);
                   //recognize end of lines as separated tokens. ttype will be set to TT_EOL
                   tokenStream.eolIsSignificant(true);
                   tokenStream.nextToken(); // get the first token
                   String flag = (String) tokenStream.sval;
                   while(flag.equals("START")){
                        System.out.println("------------------------");
                        for (int i = 0; i < 5; i++) {
                             if(tokenStream.nextToken() == tokenStream.TT_NUMBER){
                                  System.out.println((Double)tokenStream.nval);
                             else throw new Exception("Check Input File - Not " + nObjectives + " Objectives in line " + tokenStream.lineno());     
                        // check for END token...
                        if(tokenStream.nextToken() == tokenStream.TT_WORD){
                             if (tokenStream.sval.equals("END")) {
                                  flag = (String)tokenStream.sval;
              catch (Exception e) {
                   e.printStackTrace();
         }and a file to test it: save it into test.txt
    START 7106199.0 5.0376 0.5068132912149117 0.8624999999999999 225450.0
    7595899.0 4.764800000000001 0.6378176973051516 0.9624999999999999 281000.0 END
    result is:
    7106199.0
    5.0376
    0.5068132912149117
    0.8624999999999999
    225450.0
    7595899.0
    4.764800000000001
    0.6378176973051516
    0.9625 --> i dont like this value :-) it should be 0.9624999999999999
    281000.0
    is this a bug in nval? or is this normal?
    if it is normal, can someone tell me how can i get around this...
    would appreachiate any help ..
    thanks
    thomas

    that does not change anything..
    private static void readFromFileTestNumberProblem() {
              try{               
                   FileReader reader = new FileReader(new File("test.txt"));
                   StreamTokenizer tokenStream = new StreamTokenizer(reader);
                   //recognize end of lines as separated tokens. ttype will be set to TT_EOL
                   tokenStream.eolIsSignificant(true);
                   tokenStream.nextToken(); // get the first token
                   String flag = (String) tokenStream.sval;
                   tokenStream.wordChars('0', '9');
                   while(flag.equals("START")){
                        System.out.println("------------------------");
                        for (int i = 0; i < 5; i++) {
                             tokenStream.nextToken();
                             System.out.println(tokenStream.toString());
                        // check for END token...
                        if(tokenStream.nextToken() == tokenStream.TT_WORD){
                             if (tokenStream.sval.equals("END")) {
                                  flag = (String)tokenStream.sval;
              catch (Exception e) {
                   e.printStackTrace();
         }same result as before..
    7106199.0
    5.0376
    0.5068132912149117
    0.8624999999999999
    225450.0
    7595899.0
    4.764800000000001
    0.6378176973051516
    0.9625
    281000.0

  • Help!! i dont know how to make use of token from string tokenizer :(

    Hi,
    I would like to ask how to manipulate the token that are taken from string tokenizer. For example :
    input = a b 1 f 986
    supposed output:
    a
    b
    1
    f
    986
    How to make the output look like this?
    Output = (a,b,f),(1,986)
    Below is the source code of Algebra.java
    import java.io.*;
    import java.util.*;
    * Read and print, using BufferedReader from System.in, onto System.out
    public class Algebra {
    public static void main(String[] av) {
    try {
    BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
    String inputLine;
    System.out.print("Input =");
    while ((inputLine = is.readLine()) != null) {
    System.out.println("Output="+inputLine);
    StringTokenizer st =
    new StringTokenizer(inputLine, ", |");
    while (st.hasMoreElements())
    System.out.println("Token: " + st.nextElement());
    break;
    is.close();
    } catch (IOException e) {
    System.out.println("IOException: " + e);
    Thank You

    You can modify this to serve your goal.
    BufferedReader is=null;
    try {
    is = new BufferedReader(new InputStreamReader(new FileInputStream("Data.txt")));
    StreamTokenizer st = new StreamTokenizer(is);
    int n=0,k=0,q=0;
    String[] s=new String[20];
    String[] s0=new String[10];
    while(st.nextToken() != StreamTokenizer.TT_EOF) {
            switch(st.ttype) {
              case StreamTokenizer.TT_EOL:
                s[k] = new String("EOL");k++;
                break;
              case StreamTokenizer.TT_NUMBER:
                s0[q] = Double.toString(st.nval);q++;
                break;
              case StreamTokenizer.TT_WORD:
                s[k] = st.sval; // Already a String
                 k++;
                break;
              default: // single character in ttype
                s[k] = String.valueOf((char)st.ttype); k++;
            }  //end of while
       //   Some output processing here
    }catch(IOException ioe){
       ioe.printStackTrace();
    }catch(ArrayIndexOutOfBoundsException bx){
       System.err.println(bx);
    }finally{
           try{      if(is !=null)     is.close();}catch(IOException ioe){;}
    }

Maybe you are looking for