I wrote a java code, using a few method who use Scanner. the first Method worked well, but the second got stack with the error
"java.util.NoSuchElementException".
The code of the first method maxPile:
public static int maxPile() {
Scanner scan = new Scanner(System.in);
System.out.println("enter max number of piles");
int pMax = scan.nextInt();
scan.close();
return pMax;
}
the code of the second method maxMatches :
public static int maxMatches() {
Scanner scan = new Scanner(System.in);
System.out.println("enter max number of matches per pile");
int mMax = scan.nextInt();
scan.close();
return mMax;
}
The methods are identical, but the first worked, the second not... my output -
enter max number of piles
8
enter max number of matches per pile
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.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at autoNim.autoNim.maxMatches(autoNim.java:89)
at autoNim.autoNim.main(autoNim.java:12)
(the '8' is my input, line 12 call the method MaxMatches, line 89 is xint mMax=scan.nextInt();
from the method)