Anyone familar with this java error?

bubbadu

Diamond Member
Aug 30, 2001
3,551
0
0
So I have a big project do tonight at midnight, and I keep geting this error when trying to run the program.

Exception in thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(StringTokenizer.java:235)

I can post the code, but its geting pretty long.. if anyone is familar with String Tokenizer's let me know.. thanks!
 

JayHu

Senior member
Mar 19, 2001
412
0
0
are you getting it because you're not checking to see if there are any more tokensin the tokenizer to get?


 

Ameesh

Lifer
Apr 3, 2001
23,686
1
0
this is just a thought but looking at the exception, maybe there are no more tokens left in your string?

 

bubbadu

Diamond Member
Aug 30, 2001
3,551
0
0
public class PhoneMain {

public static void main(String args[]) {
//declare PhoneBook phone1
PhoneBook phone1 = new PhoneBook();

try{
//declare bufferedReader inFile to read from file phone.txt
BufferedReader inFile = new BufferedReader(new FileReader("phone.txt"));
//read one line of inFile and set equal to a string
String e1Str = inFile.readLine();
//use while loop to fill array with contents of inFile
int i = 0;
while (e1Str != null) {
//declare StringTokenizer st consisting of e1Str
StringTokenizer st = new StringTokenizer(e1Str);
//set first token of st equal to a new string for first name
String firstT = st.nextToken();
//set next token of st equal to a new string for last name
String lastT = st.nextToken();
//set next token of st equal to a new string for the number
String numT = st.nextToken();
//call method addEntry of phonebook and enter the info as a new entry
phone1.addEntry(firstT, lastT, numT);
//read next line of inFile and set equal to e1Str
e1Str = inFile.readLine();
}
 

Ameesh

Lifer
Apr 3, 2001
23,686
1
0
heres a hint, print out the value of firstT, lastT, and numT right after you read them
 

bubbadu

Diamond Member
Aug 30, 2001
3,551
0
0
I tried that, it printed each of the names, but then crashed right after that :(
 

JayHu

Senior member
Mar 19, 2001
412
0
0
well which line does it crash at?
ie specific line of code that crashes the program.
 

MikeMike

Lifer
Feb 6, 2000
45,885
66
91
which one is line 44? deffinately cant count that on here, tell us, which line it is, in relation to what you posted.

MIKE
 

screw3d

Diamond Member
Nov 6, 2001
6,906
1
76
This sounds like a simple problem to solve. In fact the problem is that StringTokenizer's nextToken() method is reading an empty token, thus returning a null. You just need to handle it like "if token=null, don't read it".

More details!
 

bubbadu

Diamond Member
Aug 30, 2001
3,551
0
0
Sorry guys... line 44 is this

String FirstName = st.nextToken();

Its taking the file, reading the firstname, and putting it into a string.
 

JayHu

Senior member
Mar 19, 2001
412
0
0
try printing the e1str after you read it inside the while loop.

maybe there is a line with a space on it in your text file, thus not returning an eof (null) for the readline method.
 

screw3d

Diamond Member
Nov 6, 2001
6,906
1
76
Originally posted by: MCrusty
You need to specify a delimeter with your Tokenizer.

No you don't need to. There is a default delimiter of all white spaces.
 

JayHu

Senior member
Mar 19, 2001
412
0
0
Originally posted by: bubbadu
Originally posted by: JayHu
Originally posted by: bubbadu
OK awesome thanks I think I just got it :) :) :)

well what was it? :)

The phone.txt file was fvcked up.. i had to redl it :p Just like JayHu said :)

cool..
just a suggestion though, you could conceivable make sure that each line has 3 tokens on it before trying to get them.
just use countTokens() to make sure it's 3 before continuing.
But if you're guaranteed to have 3 strings per line then go ahead. (i think you mentioned it was an assignment, make sure the assignment states you can assume this)