Monday, April 4, 2011

Reading Simple Input: the Scanner Class


Basic Syntax
– Attach a Scanner to System.in
– Call nextLine, nextInt, nextDouble, etc

Examples
                  Scanner inputScanner = new Scanner(System.in);
                  String s = inputScanner.nextLine();
                  int i = inputScanner.nextInt();
                 double d = inputScanner.nextDouble();

• Notes
– Scanner for System.in is useful only for simple testing
       • Use a GUI in a real application
– But you can also attach a Scanner to a File, a Socket, a Process,  and more

No comments:

Post a Comment