Scanner NoSuchElementException

Sto avendo un problema con il mio incarico Java. Sto ricevendo un’eccezione inaspettata, in particolare:

java.util.NoSuchElementException: nessuna riga trovata

Sto utilizzando Scanner(System.in) e il programma non legge continuamente nulla e ripete il testo dell’eccezione “formato non valido”. Se inserisco un int valore corretto, la prima parte passa e quindi la double parte va immediatamente in questa eccezione. Se inserisco un int valore errato, inizia a ripetere l’eccezione.

Ecco il mio codice:

 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); 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); 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()  max) System.out.println("String must be more than " + max + " characters."); else isValid = true; } while (isValid == false); return i; } }