• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with StringTokenizer

 
tyler jones
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an applet set up that specifies a parameter called bgcolor which is set up to receive an rgb value (255,34,127). What I want to do is if any of the numbers is not specified I want to replace it with a 0. But it doesn't seem to be working. In my html, I specified the parameter as "255,,34" but when I print out the values of my final array in my applet, it prints out 255,34,0 instead of 255,0,34. Can someone point out what I'm doing wrong in my method here? Thanks.
StringTokenizer bgColor = new StringTokenizer(getParameter("bgColor"),",");
for ( int i = 0; i < 3; i++ )
{
if ( bgColor.hasMoreTokens() )
background[i] = Integer.parseInt(bgColor.nextToken());
else
background[i] = 0;
}
setBackground(new Color(background[0],background[1],background[2]));
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The StringTokenizer does not recognize ,, as being two separate tokens. Multiple adjacent tokens are treated as a single token.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tyler,
There is another constructor in the StringTokenizer class that takes a boolean for the third argument.
One solution would be to use this constructor with true as the third argument, and then add a step or two to your original algorithm.
Are you getting any ideas?
 
Pradyumna Hari
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi tyler,
I implemented suggestion of Dirk and this code works as you expected.


[ May 31, 2002: Message edited by: Pradyumna Hari ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic