# Java: Comparison - Simple Input

Here's a simple program to average three ints, written using three simple input styles: JOptionPane, Scanner, and SavitchIn.

JOptionPane
  This reads and writes using dialog boxes. Because the showInputDialog method returns only a String, it has to be converted to a numeric type with, eg, Integer.parseInt().

  Documentation: Savitch, , Appendix 10, p 918. Some of the sample programs in the third edition of the textbook are rewritten in the 4th edition using JOptionPane. See Scanner below for how to download.

Scanner
  Scanner was introduced in Java 5 (JDK 1.5) and is not available in older versions of Java. It can be used for input from the console and files, but not for GUI input. It has a large number of useful methods.

  Documentation: Savitch has switched to using both JOptionPane and Scanner in the 4th edition of his textbook. You can download pdf files of the first three chapters of the new edition for free at . Follow the link to the the fourth edition ("4e"), then click on "Sample Chapters". Chapter two explains both Scanner and JOptionPane.

SavitchIn
  This proprietary class is supplied with some of Walter Savitch's Java textbooks. It is not available in standard Java and he has abandoned its use in the newer editions of his books. If you wish to use it, copy the SavitchIn.java file from the CD to the same directory as your program and compile it. Bither of the simple standard input classes (JOptionPane and Scanner) are better choices because you will come out of the course able to write real Java programs. But you're free to use it.

## JOptionPane

      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
    // File:   average3/Average3JOptionPane.java
    // Description: Average three ints.  Use JOptionPane.
    // Author: Fred Swartz
    // Date:   2002-02-13

    import javax.swing.JOptionPane;

    public class Average3JOptionPane {

        public static void main(String[] args) {

            //... Declare local variables;
            int a, b, c;  // No meaningful names are possible.
            int average;
            String temp;  // Temporary storage for JOptionPane input.

            //... Read three numbers from dialog boxes.
            temp = JOptionPane.showInputDialog(null, "First number");
            a = Integer.parseInt(temp);  // Convert String to int.
            temp = JOptionPane.showInputDialog(null, "First number");
            b = Integer.parseInt(temp);
            temp = JOptionPane.showInputDialog(null, "First number");
            c = Integer.parseInt(temp);

            //... Compute their average.
            average = (a + b + c) / 3;

            //... Display their average in a dialog box.
            JOptionPane.showMessageDialog(null, "Average is " + average);

            System.exit(0);  // Some systems may require this to stop.
        }
    }

### Scanner

      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
    // File:   average3/Average3Scanner.java
    // Description: Average three ints.  Use Scanner.
    // Author: Fred Swartz
    // Date:   2002-02-13

    // Note: Scanner was added to Java 5 (JDK 1.5).

    import java.util.Scanner;

    public class Average3Scanner {

        public static void main(String[] args) {

            //... Declare local variables;
            int a, b, c;  // No meaningful names are possible.
            int average;

            //... Initialize Scanner to read from console.
            Scanner input = new Scanner(System.in);

            //... Read three numbers from the console.
            System.out.print("Enter first number : ");
            a = input.nextInt();
            System.out.print("Enter second number: ");
            b = input.nextInt();
            System.out.print("Enter last number  : ");
            c = input.nextInt();

            //... Compute their average.
            average = (a + b + c) / 3;

            //... Display their average on the console.
            System.out.println("Average is " + average);
        }
    }

### SavitchIn

      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
    // File:   average3/Average3SavitchIn.java
    // Description: Average three ints.  Use SavitchIn.
    // Author: Fred Swartz
    // Date:   2002-02-13

    // Note: SavitchIn is NOT part of standard Java.
    //       To use it you must get it from the CD that
    //       comes with your book and copy SavitchIn.java
    //       into the same directory as this program.
    //       Compile it (once) before compiling this program.

    public class Average3SavitchIn {

        public static void main(String[] args) {

            //... Declare local variables;
            int a, b, c;  // No meaningful names are possible.
            int average;

            //... Read three numbers from the console.
            System.out.print("Enter first number : ");
            a = SavitchIn.readLineInt();
            System.out.print("Enter second number: ");
            b = SavitchIn.readLineInt();
            System.out.print("Enter last number  : ");
            c = SavitchIn.readLineInt();

            //... Compute their average.
            average = (a + b + c) / 3;

            //... Display their average on the console.
            System.out.println("Average is " + average);
        }
    }