tokenize an input text file..

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2008
Posts: 7
Reputation: minamoda is an unknown quantity at this point 
Solved Threads: 0
minamoda's Avatar
minamoda minamoda is offline Offline
Newbie Poster

tokenize an input text file..

 
0
  #1
Apr 7th, 2008
Hi to every one,
I need some help about the program i am riting. I am a beginner in java and i cant split my input text file to its tokens.
the part of the input file is :

101 Car 06AB1212 100 Y A 3
102 Car 06BG0101 200 Y M 2
202 Motor 34BB123 60 Motor
203 Motor 34CC123 20 Scooter
301 Truck 78AA321 60 B 10
403 Bus 25TT432 200 D 45
11 BerilErtan B
12 BerraCan B
1002 Eti 1983
1003 Gezitur 1999
i wrote a program and the part for tokenize is:
  1. line= BbmReader.getFileReader("input.txt").readLine();
  2. StringTokenizer tokens = new StringTokenizer(line, " ");
  3. while( tokens.hasMoreTokens()&& BbmReader.getFileReader("input.txt").inputAvailable())
  4. System.out.println(tokens.nextToken());

which only tokens one line not the others.

Please help me. Thank you all
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,514
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: tokenize an input text file..

 
0
  #2
Apr 7th, 2008
You need to have the read in a loop like so
  1. while ((line = reader.readLine()) != null){
  2. StringTokenizer tokenizer = new StringTokenizer(line," ");
  3. // do stuff
  4. }
I have no idea where the "BbmReader" class comes from, so I can't speak to any specifics on that, but the general loop should work the same.

By the way, StringTokenizer is an old legacy class and you should generally use String.split() instead.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 7
Reputation: minamoda is an unknown quantity at this point 
Solved Threads: 0
minamoda's Avatar
minamoda minamoda is offline Offline
Newbie Poster

Re: tokenize an input text file..

 
0
  #3
Apr 7th, 2008
"BbmReader" is a class in a library which we use in our school i have to use it and i think there isnt any problem with it but my program doesnt tokenize all the text file. only the first line.
And by the way, I HAVE TO use tokenizer because our teacher wants it.

Thank you so much for the help but still it doesnt do what i want.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,514
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: tokenize an input text file..

 
0
  #4
Apr 7th, 2008
It certainly will if you use the loop as I indicated. Get the reader one time and then use that loop to read lines from it.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 7
Reputation: minamoda is an unknown quantity at this point 
Solved Threads: 0
minamoda's Avatar
minamoda minamoda is offline Offline
Newbie Poster

Re: tokenize an input text file..

 
0
  #5
Apr 7th, 2008
You tell me that
  1. while ((line = reader.readLine()) != null){
  2. StringTokenizer tokenizer = new StringTokenizer(line," ");
  3. // do stuff
  4. }while ((line = reader.readLine()) != null){
  5. StringTokenizer tokenizer = new StringTokenizer(line," ");
  6. // do stuff
  7. }
works i am confused now if "reader" in here is the same as file reader in my program or not.
i wrote
  1. line= BbmReader.getFileReader("input.txt").readLine();
  2. while (line!= null){
  3. StringTokenizer tokens = new StringTokenizer(line," ");
  4. System.out.println(tokens.nextToken());}
but now it goes to an endless loop wich only writes the first token, "101"
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,514
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: tokenize an input text file..

 
0
  #6
Apr 8th, 2008
Of course it's endless - because you are not ever reading the next line. The loop that I wrote reads the line into "line" each iteration of the loop and checks it against null. Yours calls readLine() one time and keeps looping while "line" is not null - so the loop is either endless or never executes in the first place. If the syntax of the loop I wrote it confusing you, try it like this instead
  1. // I can only assume this method returns a BufferedReader
  2. BufferedReader reader = BbmReader.getFileReader("input.txt");
  3. String line = reader.readLine();
  4. while (line != null){
  5. StringTokenizer tokens = new StringTokenizer(line," ");
  6. System.out.println(tokens.nextToken());
  7. // you still need another loop here to read the rest of the tokens
  8. ...
  9. line = reader.readLine();
  10. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 3133 | Replies: 5
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC