Monday, April 4, 2011

Reading Simple Input: the Scanner Class

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

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

• Notes
– Scanner for 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