| | |
tokenize an input text file..
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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 :
i wrote a program and the part for tokenize is:
which only tokens one line not the others.
Please help me. Thank you all
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
Java Syntax (Toggle Plain Text)
line= BbmReader.getFileReader("input.txt").readLine(); StringTokenizer tokens = new StringTokenizer(line, " "); while( tokens.hasMoreTokens()&& BbmReader.getFileReader("input.txt").inputAvailable()) System.out.println(tokens.nextToken());
which only tokens one line not the others.
Please help me. Thank you all
You need to have the read in a loop like so 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.
Java Syntax (Toggle Plain Text)
while ((line = reader.readLine()) != null){ StringTokenizer tokenizer = new StringTokenizer(line," "); // do stuff }
By the way, StringTokenizer is an old legacy class and you should generally use String.split() instead.
"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.
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.
You tell me that
works i am confused now if "reader" in here is the same as file reader in my program or not.
i wrote
but now it goes to an endless loop wich only writes the first token, "101"
Java Syntax (Toggle Plain Text)
while ((line = reader.readLine()) != null){ StringTokenizer tokenizer = new StringTokenizer(line," "); // do stuff }while ((line = reader.readLine()) != null){ StringTokenizer tokenizer = new StringTokenizer(line," "); // do stuff }
i wrote
Java Syntax (Toggle Plain Text)
line= BbmReader.getFileReader("input.txt").readLine(); while (line!= null){ StringTokenizer tokens = new StringTokenizer(line," "); System.out.println(tokens.nextToken());}
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
java Syntax (Toggle Plain Text)
// I can only assume this method returns a BufferedReader BufferedReader reader = BbmReader.getFileReader("input.txt"); String line = reader.readLine(); while (line != null){ StringTokenizer tokens = new StringTokenizer(line," "); System.out.println(tokens.nextToken()); // you still need another loop here to read the rest of the tokens ... line = reader.readLine(); }
![]() |
Similar Threads
- Predict and explain the output (C)
- tokenization of file input (Java)
- Unresolved External Symbol Error (C++)
- Using strcpy and structs (C)
- read a file (Perl)
- <string> (C++)
Other Threads in the Java Forum
- Previous Thread: Applet is not displaying update image
- Next Thread: sort insert in a generic linked list
Views: 3133 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for Java
actuate android api apple applet application arguments array arrays automation balls binary bluetooth business chat class classes client code codesnippet collections component coordinates database defaultmethod doctype dragging draw ebook eclipse educational error event exception file fractal game givemetehcodez graphics gui helpwithhomework hql html ide image ingres input integer invokingapacheantprogrammatically j2me java javaprojects jmf jni jpanel julia linux list loop looping map method methods mobile mysql netbeans newbie number numbers object oracle parameter php print problem program programming project recursion scanner screen server set size sms socket sort sql string sun swing swt tcp test threads time transfer tree udp windows






