Forums Register Login

StringTokenizer

+Pie Number of slices to send: Send
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

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
Note that, as of JDK1.4, the String.split() method is preferred to StringTokenizer.
+Pie Number of slices to send: Send
 

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.
We can fix it! We just need some baling wire, some WD-40, a bit of duct tape and this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1200 times.
Similar Threads
Array problem
Sorting array's
Buffered Readers and hashtables.
Help
Array question
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 16:33:52.