Handling terminal input and output in Java

Exceptions, classes, garbage collection--is Java getting the better of you? Get your Java Beans counted with our Java Q&A column.

All developers need help from time to time. And when it comes to Java, who better to offer that help than the experienced developers at Builder? This column will feature your Java technical questions and offer recommended solutions from the Builder technical staff.

Q: How do I read a particular data type (int, long, String) from the terminal?

A: A former college professor always said that the first thing you need to figure out when learning a new language is how to get input from the keyboard and send output to the terminal. This concept may not be as important in our age of Web pages and user interfaces, but it’s still important in helping learn a language.

Within Java, you’re given two properties in the java.lang.System class that provide access to the keyboard for input and output to the terminal— and System.out.

Do you have a Java question?
Have you been struggling with some aspect of Java? Would you like to hear what the Builder technical staff suggests? Send us your Java question, and we’ll take a crack at it.

Using is an InputStream that’s available to your application and provides the capability to read input from the keyboard in a raw byte form. Unfortunately, in this form, it will probably not be very useful to you. So you should convert it into a Reader class and have it translated into characters. Using the BufferedReader class allows you to read one line at a time using its readLine() method. For instance, if you wanted to read a string from the keyboard, you could use the following code snippet:
BufferedReader lineOfText = new BufferedReader(new InputStreamReader(;
    String textLine = lineOfText.readLine();

The readLine() method will return an entire line of input into the variable textLine and may also throw an exception. As a result, the compiler will force you to either place the call to readLine() in a try/catch block looking for an IOException or declare the method within which you’re using this code with a throws construct. The throws construct lists all exceptions a method may throw.

Once you have the line of input, you can simply parse it into whatever data type you need. You might convert it into an integer with this next snippet:
int numReadIn = 0;
      try {
            numReadIn = Integer.parseInt(textLine);
      catch (NumberFormatException) {
            System.err.println(“Trouble with the parsing of the number”);

Notice that I used a method called parseInt() from the Integer class and didn’t need to instantiate a new object. If you want to read another type of number, just use the appropriate static convenience class—Double.parseDouble(),Long.parseLong(), etc. For strings, there is obviously no conversion needed. However, a NumberFormatException could be thrown if you attempt to parse an integer (as in this case) and an incompatible type, like a character, is encountered. For example, typing the string “7e4” would cause Java to throw this exception.

Using System.out
System.out is a PrintStream and can be used to send output to the terminal. It will handle the conversion of other data types to strings for you. For instance, if you wanted to read in an integer, add 1 to it, and then output it to the terminal, you would use the code snippet shown in this sidebar.

This sidebar shows a working example.


Editor's Picks

Free Newsletters, In your Inbox