Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Wiki Markup
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();

       }

}