Getting NoSuchElementException when running this method

622 Views Asked by At

My method runs, but then gets an exception after executing once. I don't understand why. Line 24, the line noted in the exception, is at "choice=in.nextInt();"

Here is my exception:

1 Find an item.
2 Display all items.
3 Update item.
4 Save item to disk.
5 Quit.
1
You chose to find an item from file.
Enter the sku of the dvd you wish to find.
857295
857295 Star.Wars 152
1 Find an item.
2 Display all items.
3 Update item.
4 Save item to disk.
5 Quit.
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at version4.version4.main(version4.java:24)

Here is my code:

import java.util.*;
import java.io.*;

public class version4 
{
public static void main(String[] args) throws FileNotFoundException
{
Scanner in = new Scanner(System.in);
boolean exit = false;
int choice = 0;

while (!exit)
{
    System.out.println("1 Find an item.\n2 Display all items.\n3 Update item.\n4 Save item to disk.\n5 Quit.");
    choice = in.nextInt();
    switch (choice){

    case 1: System.out.println("You chose to find an item from file."); findItem(); break;
    case 2: System.out.println("You chose to display all items."); readDisplay(); break;
    case 3: System.out.println("You chose to update an item."); itemUpdate(); break;
    case 4: System.out.println("You chose to save an item to disk."); itemAdd(); break;
    case 5: exit = true; in.close(); break;
    default: System.out.println("That is not a valid option."); break;
}
} 
System.out.println("Goodbye.");
} 

public static void findItem() throws FileNotFoundException {

    FileReader reader = new FileReader("read_record.txt");
    Scanner fin = new Scanner(reader);
    String str = null;
    ArrayList<String> dvdfile = new ArrayList<String>();
    if (fin != null && fin.hasNext()) { // do we have a scanner, is there anything to read?
      while (fin.hasNext()) { // while there's something to read...
        str = fin.next(); // read it.
        if (str == null) { // end if it's null
          break;
        }
        dvdfile.add(str); // otherwise add it.
      }
    } fin.close();

    Scanner kb = new Scanner(System.in);
    System.out.println("Enter the sku of the dvd you wish to find.");
    String dvdSku = kb.next();

    int index = dvdfile.indexOf(dvdSku);

    // Finds the dvd that matches sku entered, and prints out its attributes under correct rows.
    String skuToFind = dvdfile.get(index); String titleToFind = dvdfile.get(index+1); String lengthToFind = dvdfile.get(index+2);
    System.out.printf("%-10s %-15s %10s %n",skuToFind,titleToFind,lengthToFind);
    kb.close(); 
} 
1

There are 1 best solutions below

6
On BEST ANSWER

It looks like you are calling next even if the scanner no longer has a next element to provide... throwing the exception. you need to check Scanner#hasNext() before you invoke Scanner#next()

The code would be

 Scanner in = new Scanner(System.in);
    boolean exit = false;
    int choice = 0;

    while (!exit)
    {
        System.out.println("1 Find an item.\n2 Display all items.\n3 Update item.\n4 Save item to disk.\n5 Quit.");
        while(in.hasNext()){
        //next token available
        if(in.hasNextInt()){
        /next token is an int value
        choice = in.nextInt();
        }
}
}

As per the documentation

public boolean hasNext()

Returns true if this scanner has another token in its input. This method may block while waiting for input to scan. The scanner does not advance past any input.