Source of ScannerDemo.java


  1: 
  2: import java.util.Scanner;
  3: 
  4: public class ScannerDemo
  5: {
  6:     public static void main(String[] args)
  7:     {
  8:         Scanner scannerObject = new Scanner(System.in);
  9: 
 10:         System.out.println("Enter two whole numbers");
 11:         System.out.println("separated by one or more spaces:");
 12: 
 13:         int n1, n2;
 14:         n1 = scannerObject.nextInt( );
 15:         n2 = scannerObject.nextInt( );
 16:         System.out.println("You entered " + n1 + " and " + n2);
 17: 
 18:         System.out.println("Next enter two numbers.");
 19:         System.out.println("A decimal point is OK.");
 20: 
 21:         double d1, d2;
 22:         d1 = scannerObject.nextDouble( );
 23:         d2 = scannerObject.nextDouble( );
 24:         System.out.println("You entered " + d1 + " and " + d2);
 25: 
 26:         System.out.println("Next enter two words:");
 27: 
 28:         String s1, s2;
 29:         s1 = scannerObject.next( );
 30:         s2 = scannerObject.next( );
 31:         System.out.println("You entered \"" +
 32:                                                     s1 + "\" and \"" + s2 + "\"");
 33: 
 34:         s1 = scannerObject.nextLine( ); //To get rid of '\n'
 35: 
 36:         System.out.println("Next enter a line of text:");
 37:         s1 = scannerObject.nextLine( );
 38:         System.out.println("You entered: \"" + s1 + "\"");
 39:     }
 40: }