Solved

reason for java NoSuchElementException(); error

Posted on 2008-10-21
6
446 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now