File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes problem with StringTokenizer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "problem with StringTokenizer" Watch "problem with StringTokenizer" New topic

problem with StringTokenizer

tyler jones
Ranch Hand

Joined: Dec 01, 2000
Posts: 101
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());
background[i] = 0;
setBackground(new Color(background[0],background[1],background[2]));
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
The StringTokenizer does not recognize ,, as being two separate tokens. Multiple adjacent tokens are treated as a single token.

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
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?

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Pradyumna Hari

Joined: May 27, 2002
Posts: 15
Hi tyler,
I implemented suggestion of Dirk and this code works as you expected.

[ May 31, 2002: Message edited by: Pradyumna Hari ]
I agree. Here's the link:
subject: problem with StringTokenizer
It's not a secret anymore!