CS 1063, Fall 2005
Java Input Without 1.5

In case your version of JBuilder is using the 1.5 version of Java, you can use the following code to input an integer. (See also a new link off the list of lectures for CS 1063 for additional examples of the second version below: Java input without 1.5.) [Later addition: here is another page about repeated input in Java: Repeated Input in Java.]

Java input if 1.5 is available
import java.util.Scanner;

public class ScannerTest {

   public static void main(String[] args) {
      Scanner in = new Scanner(System.in);
      System.out.print("Type an integer and then 'Return' ---> ");
      int quantity = in.nextInt();
      System.out.println("You typed: " + quantity);
   }
}
Results of a run (boldface = typed input):

Type an integer and then 'Return' ---> 314159
You typed: 314159

If you don't have version 1.5 on your computer, or if you can't seem to configure JBuilder to work with 1.5, you can use the following approaches, which work with older versions of Java. In the following, the blue code above has been replaced with red below. It's a little more complicated, but you just have to copy the "magic" commands.

Java input without 1.5 (first version, not preferred)
import java.io.*;

public class BufferedReaderTest {

   public static void main(String[] args) throws IOException {
      BufferedReader in =
         new BufferedReader( new InputStreamReader(System.in));
      System.out.print("Type an integer and then 'Return' ---> ");
      String input = in.readLine();
      int quantity = Integer.parseInt(input);
      
      System.out.println("You typed: " + quantity);
   }
}

In the program above, as well as the ones below, you can just as easily read a double, using in place of int quantity = Integer.parseInt(input); the line:

You can also read in several different values, one after the other, and this is shown in detail on the link at the top of this page. Here is another sample program.

Java input without 1.5 (first version, not preferred)
import java.io.*;

public class BufferedReaderTest {

   public static void main(String[] args) throws IOException {
      BufferedReader in =
         new BufferedReader( new InputStreamReader(System.in));

      System.out.print("Type a string and then 'Return' ---> ");
      String input = in.readLine();
      System.out.println("You typed: '" + input + "'");

      System.out.print("Type a double and then 'Return' ---> ");
             input = in.readLine();
      double x = Double.parseDouble(input);
      System.out.println("You typed: " + input);

      System.out.print("Type an int and then 'Return' ---> ");
             input = in.readLine();
      int y = Integer.parseInt(input);
      System.out.println("You typed: " + input);
   }
}
Results of a run (boldface = typed input):

Type a string and then 'Return' ---> Neal R. Wagner
You typed: 'Neal R. Wagner'
Type a double and then 'Return' ---> -3.14159
You typed: -3.14159
Type an int and then 'Return' ---> 365
You typed: 365

In case you want to read numbers somewhere besides the main method, you would need to have the phrase throws IOException on every method from main down to where you are doing the input. This can be annoying, so instead you can use the following code anywhere, even buried inside a private method.

The code below also has the advantage of detecting ("catching") any problems with input that might occur. Such problems are common; this is the reason Java has structured things this way.

As before, the blue code at the beginning has been replaced with red below.

Java input without 1.5 (second version, preferred)
import java.io.*;

public class BufferedReaderTest2 {

   public static void main(String[] args) {
      BufferedReader in =
         new BufferedReader( new InputStreamReader(System.in));
      System.out.print("Type an integer and then 'Return' ---> ");
      String input = "";
      try {
         input = in.readLine();
      } catch (IOException e) {
         System.out.println("Exception reading character");
      }
      int quantity = Integer.parseInt(input);
      
      System.out.println("You typed: " + quantity);
   }
}