Keyboard Input and Console Output

How do I send output to the console?

Of course, you know all about System.out.println() and System.out.print() so it isn't necessary to say any more about those methods for outputting to the console.

A somewhat classier way to do this is to use one of the static showMessageDialog(…) methods of the JOptionPane class. These methods create pop up windows containing the messages you want to display. The downside is that the user must acknowledge the message by clicking on an "OK" button before the program will continue execution.

How do I get input from a user at a keyboard?

Unless you've redirected standard input, System.in is an InputStream that is connected to the keyboard.

As you recall, the read() method of an InputStream can only read bytes from an input stream. If you want to read characters instead of bytes, you should create a new InputStreamReader which will read bytes from the keyboard device and translate them into characters for you. However, this still limits you to reading one character at a time.

If you don't want to read single characters, but want to get an entire line of input instead, you can create a new BufferedReader and use its readLine() method. This readLine() method will aggregate characters from the source of your InputStreamReader until an end of line character is encountered will then return the entire sequence of characters to you as one Java String object.

Normally, keyboard input also requires console output because it is courteous, if not necessary, to issue a prompt to the user before data is entered.

A complication that comes along with doing most input and output is that IO Exceptions can be thrown. This can occur if a serious or fatal error occurs, and it can also happen as a part of normal processing (for example, when end-of-file is detected when doing input from a file). This complexity is passed on to the programmer who has to deal with the handling of exceptions. In situations where a chunk of coe might throw an exception, the method containing that code must either throw the exception to the next higher level (by saying this in the method signature) or handle the exception itself (using try and catch blocks). We will talk more about this later.

Another way to get input from the keyboard is to use the JOptionPane class. This gives you some simple ways to get input strings from the keyboard using pop up windows that send a prompt to the user and fetch the input. The static showInputDialog(…) methods of the JOptionPane class do this. Another nice feature is that the JOptionPane methods will never throw an IO Exception.

How can I figure out when I've read "all the input" from the keyboard?

That's not a problem unless you have some sort of input loop that allows a user to keep on entering data as long as they wish. The common solutions are to use a sentinel value (e.g. the user types "stop" or some other special value you've decided to use) or the user can enter an end-of-file character at the keyboard (ctrl-d for Unix, ctrl-z for windows). If you decide to use the end-of-file option, your input loop has to check for the end-of-file condition, since your program doesn't receive the end-of-file character itself (it is intercepted by the operating system and used to set a boolean flag whose status can be checked by your program.



Things to try
  1. Write a program the reads a byte from System.in using the System.in.read() method. Display the input value on your console.

© Wiggen & Associates, 2007