Computer Science 15-110, Spring 2010
Class Notes:  Scanner methods and Console UI


  1. Console Input
    1. Read a value
    2. Read different types
      1. Read a double value
      2. Read a String value
    3. Read until a sentinel
  2. Console User Interface (UI)
    1. Use a prompt for input
    2. Print clear and informative output
    3. Handle predictable errors

Scanner Methods and Console UI

  1. Console Input
     
    1. Read a value
      // This programs reads an int value and prints it.
      // For details, see http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html
      import java.util.Scanner;
      class MyCode {
        public static void main(String[] args) {
          Scanner scanner = new Scanner(System.in);
          int x = scanner.nextInt();
          System.out.println(x);
        }
      }

      Another Example:

      // This programs reads two int values and prints their sum.
      import java.util.Scanner;
      class MyCode {
        public static void main(String[] args) {
          Scanner scanner = new Scanner(System.in);
          int x = scanner.nextInt();
          int y = scanner.nextInt();
          System.out.println(x+y);
        }
      }
    2. Read different types
       
      1. Read a double value
        // This programs reads two double values and prints their sum.
        import java.util.Scanner;
        class MyCode {
          public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            double x = scanner.nextDouble();
            double y = scanner.nextDouble();
            System.out.println(x+y);
          }
        }
      2. Read a String value
        // This programs reads two String values and prints their concatenation.
        import java.util.Scanner;
        class MyCode {
          public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            String x = scanner.next();  // next(), not nextString()!
            String y = scanner.next();
            System.out.println(x+y);
          }
        }
    3. Read until a sentinel
      // This program reads int values until the sentinel -1,
      // and then prints out the sum (not including the sentinel!).
      import java.util.Scanner;
      class MyCode {
        public static void main(String[] args) {
          Scanner scanner = new Scanner(System.in);
          int sentinel = -1;
          int sum = 0;
          int x = scanner.nextInt();
          while (x != sentinel) {
            sum += x;
            x = scanner.nextInt();
          }
          System.out.println(sum);
        }
      }

      Or, more concisely (and without duplicate code):

      // This program reads int values until the sentinel -1,
      // and then prints out the sum (not including the sentinel!).
      import java.util.Scanner;
      class MyCode {
        public static void main(String[] args) {
          Scanner scanner = new Scanner(System.in);
          int sentinel = -1;
          int sum = 0;
          int x;
          while ((x = scanner.nextInt()) != sentinel)
            sum += x;
          System.out.println(sum);
        }
      }
  2. Console User Interface (UI)
     
    1. Use a prompt for input
      // This programs reads two int values and prints their sum.
      import java.util.Scanner;
      class MyCode {
        public static void main(String[] args) {
          Scanner scanner = new Scanner(System.in);
          System.out.print("Enter an integer: "); 
          int x = scanner.nextInt();
          System.out.print("Enter another integer: "); 
          int y = scanner.nextInt();
          System.out.println(x+y);
        }
      }
    2. Print clear and informative output
      // This programs reads two int values and prints their sum.
      import java.util.Scanner;
      class MyCode {
        public static void main(String[] args) {
          Scanner scanner = new Scanner(System.in);
          System.out.println("This program reads two values and prints their sum.");
          System.out.print("Enter an integer: "); 
          int x = scanner.nextInt();
          System.out.print("Enter another integer: "); 
          int y = scanner.nextInt();
          System.out.println(x + " + " + y + " = " + (x+y));
        }
      }
    3. Handle predictable errors
      // This programs reads two int values and prints their quotient.
      import java.util.Scanner;
      class MyCode {
        public static void main(String[] args) {
          Scanner scanner = new Scanner(System.in);
          System.out.println("This program reads two values and prints their quotient.");
          System.out.print("Enter the integer numerator: "); 
          int x = scanner.nextInt();
          System.out.print("Enter the integer denominator: "); 
          int y = scanner.nextInt();
          if (y == 0)
            System.out.println("Cannot divide by zero!");
          else
            System.out.println(x + " / " + y + " = " + (x/y));
        }
      }

carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem