This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes String Tokenizer Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String Tokenizer Question" Watch "String Tokenizer Question" New topic
Author

String Tokenizer Question

Rohan Kalbhor
Ranch Hand

Joined: Aug 18, 2006
Posts: 78
Hi guys,

I am reading a file and trying to grab some values from the file using string tokenizer
for example
if there are 3 lines in a file

a=1
b=2
c=3

i am using the following code

b1=reader


i want the output to be printed as 2.....but not able to do so
Please help.


..............................<br />Exceptions are a part of possibility<br />Errors are a part of truth<br />................................
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Don't compare strings with ==, use equals() method. That's the main bug in your program. Or you might consider equalsIgnoreCase(), if you really want "b" to match "B".

Another bug is that you call nextToken() twice in a row, without checking hasMoreTokens() in between. If a line has only one token, you'll get an exception there, which I suspect is not what you want.
[ January 23, 2008: Message edited by: Peter Chase ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Rohan Kalbhor
Ranch Hand

Joined: Aug 18, 2006
Posts: 78
Hi Peter,
the '==' case was the issue......
loads of thanks...the problem is solved
bart zagers
Ranch Hand

Joined: Feb 05, 2003
Posts: 234
Just to be sure, you are aware that the javadoc of StringTokenizer contains the following sentence?

StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: String Tokenizer Question