|
|
1. OVERVIEW |
The inpit mechanism described here was that use with Java 1.4 and is not required with respect to Java 5.
2. INPUT IN JAVA 1.4 |
Figure 1: Class diagram showing Java 1.4 API classes associated with input (remember that open arrows indicate inheritance, while closed arrows indicate association)
To facilitate input Java provides a "stream" class called BufferedReader to input characters. A class diagram indicating the relationship between this class and some other significant classes (at least in the context of input) is given in Figure 1. Note that all the classes, except the root class, shown in Figure 1 are contained (amongst others) in the java.io package. The BufferedReader class contains (amongst other things) the methods readLine and read. This first is used to input a line of text (a string), while the second is used to input a single character. In both cases a carriage return is required to signal to the Java interpreter that the input is ready. The input is placed in a temporary storage location called a buffer (something that "buffers" the program from the keyboard in a manner analogous to the way that sets of railway station buffers might be said to "buffer" trains from platforms!); hence the class name BufferedReader. To use this method we must address two issues:
To create an instance of a class we require an appropriate constructor. Inspection of the BufferedReader class shows that a constructor of the form: BufferedReader(Reader in); is provided. The formal parameter to this constructor, in, is an instance of the class (type) Reader which is a superclass of the class BufferedReader (see Figure 1). This argument is used to tell Java what the source will be. Thus in addition to a BufferedReader object we also need a Reader object. Alternatively from Figure 1 we can see that we can use an InputStreamReader object or a BufferedReader object instead because both are sub-classes of Reader. Remember that through the principle of inheritance an instance of a sub-class is also an instance of all its super classes, therefore we can substitute an inputStreamReader or a bufferedReader object for a reader object. We have already considered the BufferedReader class, any further consideration would simply result in us "going round in circles". However, inspection of the inputStreamReader class indicates the presence of a constructor that has the form: InputStreamReader(InputStream in) |
So now we need an instance of the class InputStream! Because input from the keyboard is such a common requirement Java provides an appropriate predefined InputStream object, called in, for keyboard input. This is a class field (keyword static) contained in the class System contained in the package java.lang (which also contains the object out, the instance of the PrintStream class discussed earlier in the context of output) Remember that the package jaba.lang is always compiled into every Java program. Thus we can create an InputStreamReader object as follows: InputStreamReader input = new InputStreamReader(System.in) Note that:
Now that we have an InputStreamReader object we can create an instance of the class BufferedReader. We will call it keyboardInput: BufferedReader keyboardInput = new BufferedReader(input); Alternatively we can combine the two statements as follows: BufferedReader keyboardInput = new BufferedReader(new InputStreamReader(System.in)); In the second case we create an anonymous object of type InputStreamReader (i.e. one without a name) and consequently our code becomes more succinct but less readable or understandable. Thus, for reasons of readability and understandability we will, for the time being, be adopting the first approach. We can now invoke the readLine method: keyboardInput.readLine(); This is not quite the end of the story! because although readLine has no formal parameters it returns a string (read from the keyboard) which must be assigned to an appropriately defined variable (data item). We say that the readLine method has a return type of String. Thus we need a variable of type String in which to store the input. A string variable called name would be declared as follows: String name; We can now "capture" keyboard input thus: name = keyboardInput.readLine(); Note: the type String is a predefined compound type. The nature of the type String will be discussed further at a later date. In Figure 2 the input class diagram given in Figure 1 is combined with the output class diagram presented previously. |
Figure 2: Class diagram showing Java 1.4 API classes associated with input and output
One source of (run time) error is when the user of a program inputs an exceptional value; i.e. data that is not of the expected type, or (in the case of numeric input) data that exceeds the predefined range of values specified for a numeric type. When a Java method detects an exception we say that it throws the exception. If you examine (say) the BufferedReader class you will notice that the readLine() method throws an IOException. The precise nature of this exception will be explained later, but for the time being we will simply accept that where any predefined method throws an exception you should append a "throw clause" to the signature of the method from which the methods such as readLine() are called. Thus:
public static void main(String[] args) throws IOException
Exceptions are classes the same as any other class; they are arranged in a class hierarchy descending from the class Exception, which is itself a sub class of the class Throwable.
Using the above we can extend the Hello World program described earlier so that it takes some input as indicated by the Java code presented in Table 1. Note that, in the above code, we have expressly imported the java.io package (unlike java.lang this is not included automatically) using an import statement. The '*' symbol is a "wild card" symbol indicating all the classes in the named package. The java compiler will then "compile in" those classes that are referred to our class definition. We could have specified particular classes. For example: |
Table 1: "Hello Word" version 2 |
import java.io.BufferedReader import java.io.InputStreamReader
However the effect is the same; there is no performance degradation associated with mentioning the whole package for import hence most programmers use the * wild card character when writing import statements. hence:
import java.io.*
A more sophisticated output mechanism similar to that described for input above can also be implemented.
3. WRAPPER CLASSES |
// INTEGER INPUT // Frans Coenen // Thursday 31 July 2003 // The University of Liverpool, UK // Import packages containing predefined classes import java.io.*; class IntegerInputApp { // Create BufferedReader class instance static InputStreamReader input = new InputStreamReader(System.in); static BufferedReader keyboardInput = new BufferedReader(input); /* Main method */ public static void main(String[] args) throws IOException { // Invite input System.out.print("Input an integer "); // Read in input as a string. String stringInteger = keyboardInput.readLine(); // Convert string input to data item of type Integer int inputInt = Integer.parseInt(stringInteger); // Output the result System.out.println("The value is " + inputInt); } } |
Table 2: Integer input example applications program
4. REFINEMENT OF THE INPUT PROCESS |
The above example includes a number of unnecessary data items, namely stringInteger and intInstance. We can use anonymous data item and rewrite the above as shown in Table 3. This provides a much more succinct encoding and is the approach we will commit to using in the following examples.
// INTEGER INPUT // Frans Coenen // Thursday 31 Jukly 2003 // The University of Liverpool, UK // Import packages containing predefined classes import java.io.*; class IntegerInputApp { // Create BufferedReader class instance static InputStreamReader input = new InputStreamReader(System.in); static BufferedReader keyboardInput = new BufferedReader(input); /* Main method */ public static void main(String[] args) throws IOException { // Invite input System.out.print("Input an integer "); // Read in input as a string and convert to data item of type // integer all on one line. int inputInt = Integer.parseInt(keyboardInput.readLine()); // Output the result System.out.println("The value is " + inputInt); } } |
Table 3: Refined integer input example program
In the code presented in Table 3, instead of using the parseInt(s) class method we can, alternatively, use the intValue() instance method. Using this approach requires the following steps:
The application program presented in Table 4 implements this.
// INTEGER INPUT // Frans Coenen // Monday 1 March 1999 // The University of Liverpool, UK // Import packages containing predefined classes import java.io.*; class IntegerInputApp { // ------------------- FIELDS ------------------------ // Create BufferedReader class instance public static InputStreamReader input = new InputStreamReader(System.in); public static BufferedReader keyboardInput = new BufferedReader(input); // ------------------ METHODS ------------------------ /* Main method */ public static void main(String[] args) throws IOException { // Invite input System.out.print("Input an integer "); // Read in input as a string, convert input into an instance // of type Integer and assign the value associate with the // Integer instance to the variable inputInt int inputInt = new Integer(keyboardInput.readLine()).intValue(); // Output the result System.out.println("The value is " + inputInt); } } |
Table 4: Alternative main method for "Refined integer input example" program given in Table 3
There are "value" methods for all the basic types. Whether to use a "value" instance method, or a "parse" class method is up to individuals.
Created and maintained by Frans Coenen. Last updated 10 February 2015