• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem with StringTokenizer

 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]));
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The StringTokenizer does not recognize ,, as being two separate tokens. Multiple adjacent tokens are treated as a single token.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic