Life after ConsoleReader

My textbook Computing Concepts with Java Essentials uses a simple ConsoleReader class to help students read console window input in Java programs. Other textbooks use similar classes, such as Walt Savitch's SavitchIn

Several people have asked me what students should do in CS2 if they used ConsoleReader or a similar class in CS1. I can see several solutions.

1. Keep ConsoleReader

It's short. (The complete source file is here.) It's easy to understand. It's object-oriented (no static methods). It's free. Students can just add the source file to their CS2 projects.

Sample code:

public class Sample
{
   public static void main(String[] args)
   {
      ConsoleReader console = new ConsoleReader(System.in);
      System.out.println("How old are you?");
      int age = console.readInt();
      age++;
      System.out.println("Next year, you'll be " + age);
   }
}

2. Use JOptionPane

ConsoleReader shields the student from two technical issues:

If you want to use nothing but the standard library but don't want students to deal with exception handling, use JOptionPane, like this:

String input = JOptionPane.showInputDialog("How old are you?");
int age = Integer.parseInt(input);

(Remember to import javax.swing.JOptionPane.)

Then the program pops up a dialog for each input:

{short description of image}

At the end of the program, you must call System.exit(0) to kill the UI thread.

This scheme has two drawbacks, one minor and one major:

Sample code:

import javax.swing.JOptionPane;

public class Sample
{
   public static void main(String[] args)
   {
      String input = JOptionPane.showInputDialog("How old are you?");
      age = Integer.parseInt(input);      
      age++;
      System.out.println("Next year, you'll be " + age);
      System.exit(0);
   }
}

3. Use a BufferedReader

First, declare a BufferedReader object.

BufferedReader console = new BufferedReader(
   new InputStreamReader(System.in));

Now you use the readLine method to read an input line, like this:

System.out.println("How old are you?");
String input = console.readLine();
int age = Integer.parseInt(input);

However, the readLine method generates an IOException, which is a checked exception that must be declared. Tag the method containing the call to readLine with a throws specifier, like this:

public void readInput(BufferedReader reader) throws IOException

Then tag each method that calls that method with throws IOException, until you reach main. It too should throw the exception.

public static void main(String[] args) throws IOException

There is no shame associated with acknowledging that your method might throw a checked exception—it is just “truth in advertising”.

Of course, in a really robust program, you would want to catch more exceptions. If you cover exception handling, then students can catch both the IOException and the NumberFormatException that they will encounter when reading numeric input. Frequently, this is a CS3 topic.

Sample code:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Sample
{
   public static void main(String[] args) throws IOException
   {
      BufferedReader console = new BufferedReader(
         new InputStreamReader(System.in));      
      System.out.println("How old are you?");
      String input = console.readLine();
      int age = Integer.parseInt(input);
      age++;
      System.out.println("Next year, you'll be " + age);
   }
}

4. Use BlueJ

BlueJ is a free Java environment available from Monash University. BlueJ requires a Java 2 runtime, so you need to install the JDK first before installing BlueJ. BlueJ is written in Java and runs on any platform with a Java 2 runtime, such as Linux, Solaris, Windows, or Mac OS X.

BlueJ is wonderful because it makes you think about objects and object-oriented programming, not public static void main. No input is required because students can call the constructors and methods directly. Here is a screen shot of BlueJ, constructing a sample class.

{short description of image}

Next, invoke a method:

{short description of image}

Finally, call another method to see how the object has changed.

{short description of image}

Sample code:

public class Sample
{
   public Sample(int anAge)
   {
      age = anAge;
   }

   public void nextYear()
   {
      age++;
   }

   public int getAge()
   {
      return age;
   }

   private int age;
}