This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma 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 Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String Tokenizer Question" Watch "String Tokenizer Question" New topic

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


i am using the following code


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.
I agree. Here's the link:
subject: String Tokenizer Question
It's not a secret anymore!