permaculture playing cards*
The moose likes Java in General and the fly likes StringTokenizer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "StringTokenizer" Watch "StringTokenizer" New topic
Author

StringTokenizer

Terry J Thomas
Greenhorn

Joined: Sep 15, 2004
Posts: 2
Hello All,

I am trying to create an int[] from a string that I get from my getter (9,10,11,12,13,14,15,16). I've verified that the string is passed in the format above. However when I loop to through the Tokenizer it skips values in my string. Also my new Array values are all set to 0. This is my output. Would someone point me in the right direction. Thanks

Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
Hi TJ Thomas,


System.out.println("Token Value = " + st.nextToken());
cArray[i] = Integer.parseInt(st.nextToken());


lines have bug.

You are calling nextToken() twice. So it seems like it is skipping tokens.

If you want to print you have to do something like,
String temp = stk.nextToken();
System.out.println("Token value="+temp);
cArray[i] = Integer.parseInt(temp);

Thanks
Maulin
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Maulin nailed it. And the reason your array output shows all zeros is that you loop through the array with the index "j" but print the value at position "i" which is one past your last valid value. If you only fix the first problem, you'll get an ArrayIndexOutOfBoundsException.
Terry J Thomas
Greenhorn

Joined: Sep 15, 2004
Posts: 2
Thank you both, it's working now! So my lesson tonight is to look my code.
[ September 15, 2004: Message edited by: Terry J Thomas ]
Simon Baker
Ranch Hand

Joined: Sep 09, 2004
Posts: 57
Note that, as of JDK1.4, the String.split() method is preferred to StringTokenizer.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Si Brewster:
Note that, as of JDK1.4, the String.split() method is preferred to StringTokenizer.
Thank you so much for reminding me of why I so love being at a job that uses JDKs from last century.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: StringTokenizer
 
Similar Threads
Sorting array's
Array question
Buffered Readers and hashtables.
Help
Array problem