NoSuchElementException, help please!


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: NoSuchElementException, help please!

  1. #1
    Join Date
    May 2005
    Posts
    4

    Question 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 09:26 AM.

  2. #2
    Join Date
    May 2005
    Location
    Denmark / Copenhagen
    Posts
    15
    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.

  3. #3
    Join Date
    May 2005
    Posts
    4
    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?

  4. #4
    Join Date
    May 2005
    Location
    Denmark / Copenhagen
    Posts
    15
    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.

  5. #5
    Join Date
    Oct 2004
    Posts
    311
    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.

  6. #6
    Join Date
    May 2005
    Posts
    4
    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center