0

I am currently facing some exceptions with my code and I am not sure how I am able to solve the errors. I am attempting to gather user input using the Scanner class Java but whenever I use it, the console displays:

1. Display Drivers
2. Import Infringement File
3. Generate Suspension Report
4. Save Driver Records
5. Exit Program
Enter a menu choice:
    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.nextByte(Unknown Source)
    at java.util.Scanner.nextByte(Unknown Source)
    at code.Main.validateChoice(Main.java:109) 
    at code.Main.main(Main.java:84)

I am using Eclipse, and it is displaying that nothing in my class is actually incorrect and there are no resource leaks to my understanding which can be causing this issue. I have spent a lot of time already trying to solve the issue but I can't manage to fix it.

I tested the same code and it works perfectly on another class file, but something is interfering with it in the main file. If I reference the code statically from another class file, the issue is not resolved and the same exception message displays.

Code is below:

public class Main {
  static Scanner userInput = new Scanner(System.in);

  public static void main(String[] args) {
    // Determine Driver File Location
    String fileLocation = "Driver.txt";
    File driverFile = new File(fileLocation);
    Scanner userInput = new Scanner(System.in);

    // If cannot find driver file
    while (!driverFile.exists()) {
      System.out.println("Cannot find drivers file. \nEnter the correct file location: ");
      fileLocation = userInput.nextLine(); // enter user input for file location
    }
    driverFile = new File(fileLocation);
    userInput.close();


    // Reading From Drivers
    try { // Attempt to read from file
      Scanner inputFile = new Scanner(driverFile);
      inputFile.useDelimiter(",");
      ArrayList<Driver> drivers = new ArrayList<>();

      int counter = 0; // Set counter for each iteration
      while (inputFile.hasNext()) {
        try {
          drivers.add(
              new Driver(inputFile.nextInt(), inputFile.next(), inputFile.next(), inputFile.next(),
                  inputFile.next(), inputFile.next(), inputFile.nextShort(), inputFile.nextByte()));

          // Enter correct value of last string
          String temp = inputFile.nextLine();
          if (temp.equals(",Valid")) {
            drivers.get(counter).setLicenceStatus("Valid");
          }

          else if (temp.equals(",Suspended")) {
            drivers.get(counter).setLicenceStatus("Suspended");
          }

          else { // if input was not correct, end input and show an exception has been made
            System.out.println("Licence Status incorrect, bad data in file ");
            break;
          }

          // Data check licenceClass
          if (drivers.get(counter).verifyLicenceClass() == false) {
            System.out.println("Licence Class incorrect, bad data in file ");
            break;
          }

        } catch (InputMismatchException e) {
          System.out.println("Bad data in file ");
          break;
        } // end catch
        counter++;
      }
      inputFile.close();

    } catch (FileNotFoundException e) {
      System.out.println("The driver file was not found");
    } // end missing driver file catch


    // Menu Items
    String[] firstMenuItems = {"Display Drivers", "Import Infringement File",
        "Generate Suspension Report", "Save Driver Records", "Exit Program"};
    final int firstMinMenu = 1; // menu values
    final int firstMaxMenu = 5;

    byte choice;
    do {
      displayMenu(firstMenuItems);
      choice = (byte) validateChoice(firstMinMenu, firstMaxMenu);
      // System.out.println("Your menu choice was " + choice);
    } while (choice != firstMaxMenu);


  } // end main

  /*
   * Methods
   */

  public static void displayMenu(String[] menu) {
    for (int i = 0; i < menu.length; i++) {
      System.out.println((i + 1) + ". " + menu[i]);
    }
    System.out.println("Enter a menu choice: ");
  }

  // Determine if choice is in range
  public static int validateChoice(int min, int max) {
    int input = 0;
    do {
      input = userInput.nextByte();
      if (input < min || input > max) {
        System.out.println("Please enter a menu choice in range");
      }
    } while (input < min || input > max);
    return input;
  }
}

Would appreciate any help. Thanks.

Edit: Removing the userInput.close(); near the top of the code fixed it. Also will realized I had multiple userInputs. Thanks for the replies!

Andy Lim
  • 1
  • 1

1 Answers1

0

You are doing numerous inputFile.next() when instantiating a new Driver. Each one consumes an element. Just assign it once to a variable at the beginning and use that one.

Andrea
  • 6,032
  • 2
  • 28
  • 55