-
NoSuchElementException, help please!
I have two classes in my program. The second one has a file reader method that enter the read data into an ArrayList and after that it should return to the main program located in the first class.
Everytime the program has finished reading the file and returns to the interface to recieve the next command, like this:
...
9: Read from TXT file
10: Exit program
Please select a command:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:817)
at java.util.Scanner.next(Scanner.java:1431)
at java.util.Scanner.nextInt(Scanner.java:2040)
at java.util.Scanner.nextInt(Scanner.java:2000)
at Liigatilastoija.pyydaValinta(Liigatilastoija.java:130)
at Liigatilastoija.aja(Liigatilastoija.java:39)
at Liigatilastoija.main(Liigatilastoija.java:151)
So, right as it should be recieving an integer (keyboard.nextInt()). This problem occurs only after reading a file. What am I doing wrong??
Last edited by Erbe; 05-24-2005 at 08:26 AM.
-
Hi
This exception is a runtime exception that are being thrown to indicate that the enumeration has no more elements - so it seems that you are calling nextElement() after the end of the enumeration.
But it is difficult to say without the code - but I hope this can help you.
-
The end of the file reader method:
}
catch (IOException e) {
System.out.println("Error reading file!");
}
finally {
if(reader != null){
try {
reader.close();
}
catch (IOException e1) {
System.out.println("Error closing file.");
}
}
}
}
After this the program returns to the interface and asks for nextInt, but crashes. Is there a problem in the code above or should I look elsewhere?
-
I can not see any problems in the code but I think that the problem is the nextInt - Try write a debug message just before your nextInt to see if the enumeration is at the end. Make a test before you call nextInt to avoid calling it after the end of the enumeration.
Don't know if this is of any help to you.
-
try using the enum in the folowing context:
while(enum.hasMoreElements())
{
enum.nextInt();
}
this way, the nextInt() will never be called after the enum is emtpy.
-
Thanks for the help guys, I already fixed the problem.
I just changed the type of the file name reader (in the file reader method) to Scanner instead of BufferedReader. Don't really know why that helped but thank god it did.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks