?
Solved

reason for java NoSuchElementException(); error

Posted on 2008-10-21
6
Medium Priority
?
456 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 60

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 60

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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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 …
Suggested Courses
Course of the Month13 days, 21 hours left to enroll

801 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