Link to home
Start Free TrialLog in
Avatar of ohjava
ohjavaFlag for United States of America

asked on

reason for java NoSuchElementException(); error

I just ran a piece a program in java and it is supposed to read words from an input file.
But instead it gives me the following errors. The code has no errors in it. I want to know
what the reason may be for this error message. I am attaching a zip of the files that I am using. I got the same message both at the command line as well as NetBeans IDE. Please note that I renamed .java to .txt.
Also please note that the input file may be need to have the line numbers. That is line numbers can be deleted. Only text should be fine. Finally when I ran the program with or without the file having line numbers the message I got was as follows:- (This one I copied and pasted from NetBeans 6.1 output window). I am new to java, so can anyone please tell me why I am getting this error? Please note that the code was given by the prof and is supposed to be right.It compiles just fine.The output is supposed to be an index (unsorted) of words in the file with line numbers they appear on.
THE OUTPUT (COPIED AND PASTED)
==================================================================================
init:
deps-jar:
compile:
run:
1.
Exception in thread "main" java.util.NoSuchElementException
        at java.util.StringTokenizer.nextToken(StringTokenizer.java:332)
        at lab4d.Token.getToken(Token.java:42)
        at lab4d.lab4d.main(lab4d.java:20)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

lab4d.zip
Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

Here is the official explanation, since you are learning for school it appears:
"Thrown by the nextElement method of an Enumeration to indicate that there are no more elements in the enumeration."
http://java.sun.com/j2se/1.4.2/docs/api/java/util/NoSuchElementException.html

I would examine your iteration through tokens and consider how you would handle when you have no more.
ASKER CERTIFIED SOLUTION
Avatar of javaexperto
javaexperto
Flag of Mexico image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ohjava

ASKER

Hey Members Thanks for the feedback. I am examining the answers. I will try and correct these on my own.If I cannot I will aske for more help. So will this work then if the "problem statement" is in the while loop as is the return true statement? And then outside of the while I add a "return false;' statement? I will try this and let you know if it works or does not. All your feedbacks are very much appreciated.

Avatar of ohjava

ASKER

Hey I messed up while copying the code. Either the above solution by javaexperto works or check the following. I forget to type in the "!" operator in the while loop condition.
public boolean getToken()
{
String s;
while(!tokens.hasMoreTokens()) //forgor to type in "!" before "tokens"
        {
        if (cisio.eof()) return false;
        s = cisio.GetLine();
        if (cisio.eof()) return false;
        tokens = new StringTokenizer(s);
        linenr +=1;
        System.out.println(linenr+". "+s);
        }
 
token = tokens.nextToken(); //here is the problem
return true;
}

Open in new window

You need to put tokens = nextToken() inside the while loop. But also you need to asign first your read string to the StringTokenizer because !tokens.hasMoreTokens() will work only the first time when tokens hasn't tokens.
So I would do this: first read the file line, then assign it to the StringTokenizer and the put the while something like this:


public boolean getToken()
{
String s;
if (!cisio.eof()) {
    s = cisio.GetLine();
    tokens = new StringTokenizer(s);
} else {
    return false;
}
while(tokens.hasMoreTokens()) //without "!" only will work first time
        {
        token = tokens.nextToken();
        linenr +=1;
        System.out.println(linenr+". "+s);
        }
 
return true;
}

Open in new window