java.util.NoSuchElementException - Scanner reading user input java.util.NoSuchElementException - Scanner reading user input java java

java.util.NoSuchElementException - Scanner reading user input


This has really puzzled me for a while but this is what I found in the end.

When you call, sc.close() in first method, it not only closes your scanner but closes your System.in input stream as well. You can verify it by printing its status at very top of the second method as :

    System.out.println(System.in.available());

So, now when you re-instantiate, Scanner in second method, it doesn't find any open System.in stream and hence the exception.

I doubt if there is any way out to reopen System.in because:

public void close() throws IOException --> Closes this input stream and releases any system resources associated with this stream. The general contract of close is that it closes the input stream. A closed stream cannot perform input operations and **cannot be reopened.**

The only good solution for your problem is to initiate the Scanner in your main method, pass that as argument in your two methods, and close it again in your main method e.g.:

main method related code block:

Scanner scanner = new Scanner(System.in);  // Ask users for quantities PromptCustomerQty(customer, ProductList, scanner );// Ask user for payment methodPromptCustomerPayment(customer, scanner );//close the scanner scanner.close();

Your Methods:

 public static void PromptCustomerQty(Customer customer,                              ArrayList<Product> ProductList, Scanner scanner) {    // no more scanner instantiation    ...    // no more scanner close } public static void PromptCustomerPayment (Customer customer, Scanner sc) {    // no more scanner instantiation    ...    // no more scanner close }

Hope this gives you some insight about the failure and possible resolution.


The problem is

When a Scanner is closed, it will close its input source if the source implements the Closeable interface.

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html

Thus scan.close() closes System.in.

To fix it you can make

Scanner scan staticand do not close it in PromptCustomerQty. Code below works.

public static void main (String[] args) {   // Create a customer// Future proofing the possabiltiies of multiple customersCustomer customer = new Customer("Will");// Create object for each Product// (Name,Code,Description,Price)// Initalize Qty at 0Product Computer = new Product("Computer","PC1003","Basic Computer",399.99); Product Monitor = new Product("Monitor","MN1003","LCD Monitor",99.99);Product Printer = new Product("Printer","PR1003x","Inkjet Printer",54.23);// Define internal variables // ## DONT CHANGE ArrayList<Product> ProductList = new ArrayList<Product>(); // List to store ProductsString formatString = "%-15s %-10s %-20s %-10s %-10s %n"; // Default format for output// Add objects to listProductList.add(Computer);ProductList.add(Monitor);ProductList.add(Printer);// Ask users for quantities PromptCustomerQty(customer, ProductList);// Ask user for payment methodPromptCustomerPayment(customer);// Create the headerPrintHeader(customer, formatString);// Create BodyPrintBody(ProductList, formatString);   }static Scanner scan;public static void PromptCustomerQty(Customer customer, ArrayList<Product> ProductList)               {// Initiate a Scannerscan = new Scanner(System.in);// **** VARIABLES ****int qty = 0;// Greet CustomerSystem.out.println("Hello " + customer.getName());// Loop through each item and ask for qty desiredfor (Product p : ProductList) {    do {    // Ask user for qty    System.out.println("How many would you like for product: " + p.name);    System.out.print("> ");    // Get input and set qty for the object    qty = scan.nextInt();    }    while (qty < 0); // Validation    p.setQty(qty); // Set qty for object    qty = 0; // Reset count}// Cleanup}public static void PromptCustomerPayment (Customer customer) {// VariablesString payment = "";// Prompt Userdo {System.out.println("Would you like to pay in full? [Yes/No]");System.out.print("> ");payment = scan.next();} while ((!payment.toLowerCase().equals("yes")) && (!payment.toLowerCase().equals("no")));// Check/set resultif (payment.toLowerCase() == "yes") {    customer.setPaidInFull(true);}else {    customer.setPaidInFull(false);}}

On a side note, you shouldn't use == for String comparision, use .equals instead.


You need to remove the scanner closing lines: scan.close();

It happened to me before and that was the reason.