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!

StringTokenizer

Status
Not open for further replies.

JTan

Technical User
Oct 9, 2001
73
0
0
SG
Hi all,

I encounter the error java.util.NoSuchElementException when my program runs.

The error occurs at the codes below:

StringTokenizer reader = new StringTokenizer (line, "\n\t\r ");

..
while (reader.hasMoreTokens()) {

System.out.println(reader.nextToken());
System.out.println(reader.nextToken());
..

The first element can be printed out during runtime but the error would appear directly after that.

I am wondering if there is anything wrong with my codes above.

My input file
==============

JOB
ARRIVAL 20
CPU 50
IO 50
CPU 50
IO 50
END
JOB
ARRIVAL 30
CPU 150
IO 250
END
JOB
ARRIVAL 1500
CPU 50
IO 250
CPU 50
IO 250
CPU 50
IO 250
CPU 50
IO 250
CPU 50
IO 250
CPU 50
IO 250
END
 
You called reader.nextToken() twice in your loop, meanwhile some of the entries in your input file have only one token, maybe if you delete the second call to nextToken(), it might work
 
I would think it would be conceptually clearer (and actually a bit easier to code) to use two StringTokenizers, one that uses "\r\n" for its token and creates full-line tokens, and then another that uses "\t " and breaks those lines into pieces. Then you'd have a clear mechanism for finding out if each "line" has one or two "pieces" and can adjust the handling accordingly.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top