So far we have only looked at programs which involve output only. There are many applications where we are required to pass some data into the program in the form of input. Here we will examine the mechanism provided by Java to achieve input. The discussion will also serve as a useful revision of the OO features of Java (and OO languages in general).
Figure 1: Class diagram showing API classes associated with input
To facilitate input Java provides a class called Scanner. 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 the classes System and Object are contained in the package java.lang, InputStream in the package java.io, and the class Scanner in the package java.util.
The Scanner class contains (amongst other things) the methods next which is used to input a token. A token, by default, is any sequence of characters delimitted by "while space". Note that a carriage return is required to signal to the Java interpreter that the input is ready for collection.
To use this method we must address two issues:
To create an instance of a class we require an appropriate constructor. Inspection of the Scanner class shows that a constructor of the form:
is provided. The formal parameter to this constructor, source, is an instance of the class (type) InputStream. So 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 java.lang is always compiled into every Java program. Thus we can create a Scanner object as follows:
private Scanner input = new Scanner(System.in);
Now that we have a Scanner object we can invoke the next method:
This is not quite the end of the story, because although next 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 next 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 name1 would be declared as follows:
We can now "capture" keyboard input thus:
name1 = input.next();
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 API classes associated with input and output
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 thecode, we have expressly imported the java.util 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 in our class definition. We could have specified particular classes. For example:
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:
Table 1: "Hello Word" version 2
For anybody that may be interested some notes on how input is achieved using Java 1.4 are available.
3. NUMERIC INPUT
In the above example we have shown how we can input a string token into a Java program using the next method. Using this method input is always captured in the form of a string; so what if we wish to input (say) an integer or a float? To do this Java provides a number of specialised methods, one for each primitive type. For example nextInt() returns an int, nextFloat() returns a floating point number (float or a dounle), etc.
The example application program presented in Table 2 illustrates the process of inputting an integer. If (after compilation) we run this program we will be requested for some input, which will then be echoed to the screen as follows:
$ java5 IntegerInputApp Input two integers 23 -47 The values are 23 and -47
Try inputting a very large number such as 1000000000000. This will generate an exception because this number is larger than the maximum defined for the integer type:
Table 2: Integer input example applications program
$ java IntegerInputApp Input two integers 23 -12345678901234567890 Exception in thread "main" java.util.InputMismatchException: For input string: "-12345678901234567890" at java.util.Scanner.nextInt(Scanner.java:2046) at java.util.Scanner.nextInt(Scanner.java:2000) at IntegerInputApp.main(IntegerInputApp.java:25)
Note the InputMismatchException (do not worry at this stage if you do not understand all of the resulting output.)
4. FORMATTED OUTPUT
Instead of the output given in Tables 1 and 2:
System.out.print("\nHello " + name1 + " " + name2); System.out.println(inputInt1 + " and " + inputInt2);
We could have used the printf method from the PrintStream class, and written:
System.out.printf("\nHello %s %s",name1,name2); System.out.printf("%d and %d\n",inputInt1,inputInt2);
(The efeect would be the same.) The printf ("print formatted") method can have any number of arguments the first of which must be a format sting. The format string will include format specifiers, one for each of the remaining arguments, which specify how each argument should be output. Format specifiers typically comprise the following:
If we replace the output statements included in Table 2 with:
System.out.print("The values are: "); System.out.printf("%010d and %010d\n",inputInt1,inputInt2); System.out.printf("%-10d\n",inputInt1); System.out.printf("%-10d\n",inputInt2); System.out.printf("%10d\n",inputInt1); System.out.printf("%10d\n",inputInt2);
and run the application again we will get:
$ java IntegerInputApp Input an integer 23 56789 The values are: 0000000023 and 0000056789 23 56789 23 56789
Further information regarding format stings can be obtained from the the Sun Java WWW site.
Created and maintained by Frans Coenen. Last updated 10 February 2015