Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String Tokenizer Question

 
Rohan Kalbhor
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Rohan Kalbhor
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Peter,
the '==' case was the issue......
loads of thanks...the problem is solved
 
bart zagers
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic