This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread 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 Android Security Essentials Live Lessons this week in the Android 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.
 
 
subject: String Tokenizer Question
 
Similar Threads
Question on String Tokenizer .
Doubt about StringTokenizer !!
sting tokenizer in a loop(vv urgent)
Problem using StringTokenizer
where this program goes wrong(using 2D array)?