Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String Tokenizer Problem 1

Status
Not open for further replies.

jisoo22

Programmer
Apr 30, 2001
277
0
0
US
Hello all,

I have a slight issue with the String Tokenizer class. Here's what I have, I'm parsing a text file like so:

10 9 7
6 8 9
11 4 13

A few rows of 3 numbers each. I have the string tokenizer running and it picks up the first two numbers in each row but I always get a zero for the last numbers. Here's a snippet of my code:

Code:
			while(line != null)
			{
				tokenizer = new StringTokenizer(line);
				token = tokenizer.nextToken();

					switch(lineNum)
					{
						// parses for first line
						case 1:
							casenum = 0;
							while(tokenizer.hasMoreTokens())
							{
								switch(casenum)
								{
									// parses for first number
									case 0:
										temp = Integer.parseInt(token);
										fl.setCompact(temp);
										casenum++;
										break;
									case 1:
										temp = Integer.parseInt(token);
										fl.setMid(temp);
										casenum++;
										break;
									case 2:
										temp = Integer.parseInt(token);
										fl.setVan(temp);
										casenum++;
										break;

								}
								token = tokenizer.nextToken();
							}
							break;

As you can see, I have a while loop with a switch statement inside, then within each of the cases (there are 3) there is another nested while loop with another switch statement inside. Hence the larger while loop/switch statement combo looks at the lines while the nested while loop/switch statement combos get the individual tokens within each line and turn them into numbers. Those numbers are then stored in variables in another class. Does anyone have any idea what's going on? I just can't seem to get that last set of numbers.

Thanks,
Jisoo22
 
Step 1:
while(line != null)
{
tokenizer = new StringTokenizer(line);
token = tokenizer.nextToken();
instead of this :
while(line != null)
{
tokenizer = new StringTokenizer(line);
/********************************************/
Step 2:
while(tokenizer.hasMoreTokens())
{
switch(casenum)
instead of this:
while(tokenizer.hasMoreTokens())
{
token = tokenizer.nextToken();
switch(casenum)
/******************************************/
Step 3:
token = tokenizer.nextToken();
}
break;
instead of this:
}
break;

You seem to misuse the hasmoreTokens. method...


Salih Sipahi
Software Engineer.
City of Istanbul Turkey
s.sipahi@sahinlerholding.com.tr
turkey_clr.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top