# Java: Alternatives to SavitchIn

## Objection to SavitchIn

Non-standard. The Savitch textbook is good, but has one objectionable aspect - Instead of using standard Java to get input from the user, he has written a proprietary class called SavitchIn to read from the console input stream. You will never see this class used anywhere else. If you use it in this class, you will have to forget about it and learn the standard way to doing things. I propose we drop it and learn one of the easy, standard ways to do input.

Scanner, but.... Before Java 5 (JDK 1.5), it was awkward to read from the console, so his intentions were good in making it easier for students. Console input can be done using the Java 5 java.util.Scanner class. However, this only works if you have installed Java 5, which was not on the book CD. Some students probably don't have adequate download access, so this alternative is unacceptable. Anyway, there's a better way using graphical dialog boxes.

Before we go into the (simple) details of dialog boxes, here are two simple "first" programs to test your installed software with. These should be used in preference to the example on page 23 of the textbook.

## First programs

Here are two programs here that you can use to test your development software installation (eg, TextPad). Copy from these examples, and paste them into TextPad (or whatever). Save them in a file with the same name as the class plus ".java", compile and run them. They show the difference between console output and dialog box output. Everything in these programs will be explained, so I don't expect you to understand them yet. This is just a test for your development system.

### Console output

    // File:        cmis102a/ch01_helloConsole/HelloConsole.java
    // Description: First console program.
    // Author:      Fred Swartz
    // Date:        2005-Jan-26

    public class HelloConsole {
        public static void main(String[] args) {
            System.out.println("Hello in the console output stream!");
        }
    }

This should produce a DOS command window with the message in it.

### Dialog box output

    // File:        cmis102a/ch01_helloDialog/HelloDialog.java
    // Description: First dialog box program.
    // Author:      Fred Swartz
    // Date:        2005-Jan-26

    import javax.swing.*;

    public class HelloDialog {
        public static void main(String[] args) {
            JOptionPane.showMessageDialog(null, "Hello in a dialog box!");

            System.exit(0);  // Make sure program is stopped.
        }
    }

If you compile and run this in TextPad it will produce a DOS command window, altho there is no program output in it. This window is entirely an artifact of TextPad and would not normally appear when running a GUI-based Java program.

## Use SavitchIn if you wish

It really doesn't matter which style of input/output (I/O) you use in this class.

- Console I/O using SavitchIn for input.
- Console I/O using Scanner for input (requires Java 5)
- Dialog box I/O.

I'll supply notes on dialog box I/O and will be using it in my examples, so you will get used to reading it, but write your programs using any of the above schemes.