Scanner NoSuchElementException

I have a problem with Java assignment. I get an unexpected exception, in particular:

java.util.NoSuchElementException: row not found

I use Scanner(System.in) , and the program constantly does not read anything and repeats the text of the exception "invalid format". If I enter a correctly evaluated int , the first part will go through, and then the double part will immediately go into this exception. If I introduce an incorrectly evaluated int , then it will start an exception loop.

Here is my code:

 import java.util.Scanner; public class Program_4 { public static void main(String[] args) { getValidInt("Enter an integer from 5 to 50",5,50); getValidDouble("Enter a double from 5.0 to 50.0",5.0,50.0); getValidString("Enter a string with length from 5 to 8 characters",5,8); } public static int getInt(String prompt) { Scanner sc = new Scanner(System.in); int i = 0; boolean isValid; do { try { System.out.print(prompt + ": "); i = Integer.parseInt(sc.nextLine()); isValid = true; } catch (Exception e) { System.out.println(e); System.out.print("Invalid Format: "); isValid = false; } } while (isValid == false); sc.close(); return i; } public static int getValidInt(String prompt, int min, int max) { int i = 0; boolean isValid = false; do { i = getInt(prompt); if(i < min) System.out.println("Value must be >= " + min); else if(i > max) System.out.println("Value must be <= " + max); else isValid = true; } while (isValid == false); return i; } public static double getDouble(String prompt) { Scanner sc = new Scanner(System.in); double i = 0.0; boolean isValid; do { try { System.out.print(prompt + ": "); i = Double.parseDouble(sc.nextLine()); isValid = true; } catch (Exception e) { System.out.println(e); System.out.println("Invalid Format: "); isValid = false; } } while (isValid == false); sc.close(); return i; } public static double getValidDouble(String prompt, double min, double max) { int i = 0; boolean isValid = false; do { i = getInt(prompt); if(i < min) System.out.println("Value must be >= " + min); else if(i > max) System.out.println("Value must be <= " + max); else isValid = true; } while (isValid == false); return i; } public static String getString(String prompt) { Scanner sc = new Scanner(System.in); String i=""; boolean isValid; do { try { System.out.print(prompt + ": "); i = sc.nextLine(); isValid = true; } catch (Exception e) { System.out.print("Invalid Format: "); isValid = false; } } while (isValid == false); sc.close(); return i; } public static String getValidString(String prompt, int min, int max) { String i; boolean isValid = false; do { i = getString(prompt); if(i.length() < min) System.out.println("String must be more than " + min + " characters."); else if(i.length() > max) System.out.println("String must be more than " + max + " characters."); else isValid = true; } while (isValid == false); return i; } } 
+7
source share
2 answers

You have more than one Scanner that you close by closing the underlying InputStream , so another Scanner can no longer read InputStream and NoSuchElementException from the same results.

For console applications, use one Scanner to read from System.in .

+14
source

Since you print the same message in all three places where an exception is thrown, it’s hard to say with any certainty what happens:

  • Use printStackTrace() to find out where the exception occurs.

  • Do not catch Exception . Understand the exceptions you expect and that your code is meant to be handled. If you catch an Exception , you may end up catching all kinds of unexpected exceptions (NPE, end of file, etc.) ... and reporting them incorrectly as "Invalid format."

+2
source

All Articles