Solved

reason for java NoSuchElementException(); error

Posted on 2008-10-21
6
453 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 59

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 59

Expert Comment

by:Kevin Cross
ID: 22768244
0
 
LVL 6

Accepted Solution

by:
javaexperto earned 250 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Protect jar file - windows app 2 65
How to convert String matching to regex in java 4 49
sql import cannot be resolved jsp 3 47
Java program running SQL query 5 37
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

861 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