Skip to content

Instantly share code, notes, and snippets.

@ksnortum
Created February 5, 2018 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ksnortum/22eeae450122a1112d3e6c1a674359e2 to your computer and use it in GitHub Desktop.
Save ksnortum/22eeae450122a1112d3e6c1a674359e2 to your computer and use it in GitHub Desktop.
Don't close Scanner when opened to System.in
package net.snortum.play;
import java.util.Scanner;
public class ScannerClosePlay {
public static void main(String[] args) {
Scanner scanner1 = new Scanner(System.in);
System.out.print("Enter foo: ");
String foo = scanner1.nextLine();
scanner1.close();
Scanner scanner2 = new Scanner(System.in);
System.out.print("Enter bar: ");
String bar = scanner2.nextLine(); // Exception
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment