?
Solved

reason for java NoSuchElementException(); error

Posted on 2008-10-21
6
Medium Priority
?
461 Views
Last Modified: 2013-11-23
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
0
Comment
Question by:ohjava
  • 2
  • 2
  • 2
6 Comments
 
LVL 61

Expert Comment

by:Kevin Cross
ID: 22768234
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.
0
 
LVL 61

Expert Comment

by:Kevin Cross
ID: 22768244
0
 
LVL 6

Accepted Solution

by:
javaexperto earned 1000 total points
ID: 22768534
the problem is that in your method getToken you call  tokens.nextToken(); outside of the while loop:
while(tokens.hasMoreTokens()) so when the while ends you make the call nextToken() and there in no more tokens, the the error noSuchELementException

public boolean getToken()
{
String s;
while(tokens.hasMoreTokens())
        {
        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

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ohjava
ID: 22768591
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.

0
 

Author Comment

by:ohjava
ID: 22768648
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

0
 
LVL 6

Expert Comment

by:javaexperto
ID: 22769068
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

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month9 days, 5 hours left to enroll

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question