I try to read an int and after it a string, in this way:
String wantA = "";
Scanner in = new Scanner(System.in);
System.out.println("Enter A");
wantA = in.nextLine();
in.close();
// some code
int want = 0;
Scanner in = new Scanner(System.in);
System.out.println("Save? Press 1 for yes, or 0 for no");
want = in.nextInt();
in.close();
after it prints
Save? Press 1 for yes, or 0 for no
then I get
java.util.NoSuchElementException
How can I fix it?
Remove
in.close();– it is killing the input stream (which never gets reopened).Instead just keep using the same
Scanner.Change your code to this: