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.

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.

My recommendation - Use JOptionPane for simple input

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