This is my working code.
public static void play() {
Scanner myScanner = new Scanner(System.in);
Scanner myOtherScanner = new Scanner(System.in);
while (!endCondition()) {
//stuff
shoot(myScanner);
}
keepPlaying(myOtherScanner);
myScanner.close();
myOtherScanner.close();
}
Method receiving myScanner:
static void shoot(Scanner sc) {
int bullet;
//Stuff
bullet = Integer.parseInt(sc.next());
//More stuff
}
Method receiving myOtherScanner:
static void keepPlaying(Scanner myOtherScanner) {
//Stuff
int option = Integer.parseInt(myOtherScanner.next());
//More stuff
}
Now what I don't understand:
If I close myScanner before calling keepPlaying(myOtherScanner), myOtherScanner.next() will throw:
NoSuchElementException
From what I've found it seems that closing a single Scanner closes System.in. Is that correct?
What is a good way to go around it? Closing only at the very end?
Is it better to use a single Scanner?
Do I use the Scanners as global class elements?
Do I just not close the Scanners?