Converting from the book's input style to ours.

This document describes how to convert the example programs in the book (using the Keyboard class) into programs that don't use the Keyboard class.We are using a different style because the Keyboard class is specific to this textbook. We want you to be able to write programs that will work with any Java compiler without having to rely on the Keyboard class as a crutch.

IMPORTANT

First, there is one important thing to keep in mind. The input style we are using for this class only lets you enter one number on a line. So, in the book they sometimes show in an example execution something like this (see p. 118):
Enter three integers:
45 22 69
What this means is that when you run the program after changing it using these instructions, you'll have to hit <return> after each number. So your input will have to look like this:
Enter three integers:
45
22
69

How to do the conversion

  1. Replace the import statement

  2. Remove the import statement from the file that says:
      import cs1.Keyboard;
    Instead, add this import statement:
      import java.io.*;
  3. Change the method declaration for main

  4. You need to change it from:
      public static void main (String[] args)
    to this:
      public static void main (String[] args) throws IOException
  5. Add the setup code

  6. Add the following to the beginning of each method (usually just main) that uses the Keyboard methods:
      BufferedReader stdin = new BufferedReader(
        new InputStreamReader(System.in));
  7. Replace the Keyboard methods with our methods:

  8.  
    Data type 
    What is in the book
    What you should replace it with
    String
    Keyboard.readString(); stdin.readLine();
    int
    Keyboard.readInt(); Integer.parseInt(stdin.readLine());
    double
    Keyboard.readDouble(); (new Double(stdin.readLine())).doubleValue();