Creating multiple scanners on System.in upsets the predictability of program behavior, especially when System.in has been re-directed.
Noncompliant Code Example
import java.util.Scanner;
public class InputLibrary\{
public static int getInt()\{ Â Â Â Â Â System.out.println("Please enter an int:"); Â Â Â Â Â Scanner in = new Scanner(System.in); Â Â Â Â Â return in.nextInt(); Â Â \}
public static int getDouble()\{ Â Â Â Â Â System.out.println("Please enter a double:"); Â Â Â Â Â Scanner in = new Scanner(System.in); Â Â Â Â Â return in.nextDouble(); Â Â \}
\}
Compliant Code Example
import java.util.Scanner;
public class InputLibrary{
private static Scanner in = new Scanner(System.in);
public static int getInt(){ Â Â Â Â Â System.out.println("Please enter an int:"); Â Â Â Â Â return in.nextInt(); Â Â }
public static int getDouble(){ Â Â Â Â Â System.out.println("Please enter a double:"); Â Â Â Â Â return in.nextDouble(); Â Â }
} |