File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

StringTokenizer

 
Terry J Thomas
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1873
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1646
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that, as of JDK1.4, the String.split() method is preferred to StringTokenizer.
 
David Harkness
Ranch Hand
Posts: 1646
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic