# Java Notes

# Keyboard Input

There are two approaches to getting keyboard input from the user.

1. GUI (Graphical User Interface). Displaying a graphical text box to type into is the how standard programs communicate.
2. Console. Console I/0 is the old-fashioned style of interaction where the human and computer type text back and forth to each other - basically a screen version of the old Teletype machine. Java was designed for graphical user interfaces (GUI) and large programs, and no attempt was made in the beginning to have simple keyboard input, eg, something like C++'s `cin`.

   Java 5 (introduced around the beginning of 2005) introduced the java.util.Scanner class, which made console I/O easy for the first time. It's also very useful for reading from text files.

## Graphical User Interface

This is how normal programs interact - windows, buttons, menus, etc.

- Full-strength GUIs require a bit of programming. They aren't really very hard, but they do require knowing things that would be too much for the very first programs.
  - Advantages: This is what you want your programs to look like.
  - Disadvantages: Too much coding to start. Fortunately, it's easy to use the simple dialog boxes.
- Dialog boxes using JOptionPane.showInputDialog.
  - Advantages: Easy to use.
  - Disadvantages: Limited to text fields (no menus, etc).

## Console input - reading from System.in

Console input is rarely used in real programs, so why do authors like to use it? There are probably two reasons: (1) It was the only standard way in Pascal, C, C++, etc. This is how the authors learned to program, and they often have written pre-Java textbooks which they've adapted to Java.

- Proprietary classes (eg, SavitchIn, ...). Many authors have struggled with how do simple input. If they're determined to use console input, there was no alternative to writing their own classes.
  - Advantages: Easier than BufferedReader.
  - Disadvantages: The are unique to each author and are never used anywhere else.
- java.io.BufferedReader
  - Advantages: It runs in all versions of Java.
  - Disadvantages: It requires a lot of programming. Console input is awkward..
- java.util.Scanner
  - Advantages: It's easy to use.
  - Disadvantages: It wasn't available until Java 5, so may not be available, and it's still coming from the console.

## My recommendation - Use JOptionPane for simple input

You really want to have a GUI interface, don't you? JOptionPane methods are easy and standard.