Section 2.4
The Console Class
FOR SOME UNFATHOMABLE REASON, Java seems to lack any reasonable built-in subroutines for reading data typed in by the user. You've already seen that output can be displayed to the user using the subroutine System.out.print. This subroutine is part of a pre-defined object called System.out. The purpose of this object is precisely to display output to the user. There is a corresponding object called System.in that exists to read data input by the user, but it provides only very primitive input facilities. (And it is not even implemented in some Java programming environments, such as CodeWarrior 11 on the Macintosh.)
There is some excuse for this, since Java is meant mainly to write programs for Graphical User Interfaces, and those programs have their own style of input/output, which is implemented in Java. However, better support is needed for input/output in old-fashioned non-GUI programs. Fortunately, is possible to extend Java by creating new classes that provide subroutines that are not already built into Java. If your program has access to a class created by you or by someone else, you can use the routines defined in that class in your program. You don't even have to understand the details of how they work. This is, of course, the whole idea behind building on and reusing previous work.
As an example, I've written a class called EasyInput that defines a few subroutines for easily reading values input by the user. The subroutines in this class make it possible to get input from the standard input object, System.in, without knowing about certain advanced aspects of Java that you would need to know to use System.in directly.
To use the EasyInput class, you must make sure that the class is available to your project. (If you are using a programming environment such as Visual J++, this can be done by adding the source file, EasyInput.java, to your programming project.) Let's assume that you've made EasyInput available to your program and that you want to use it to read an integer typed in by the user. The value that the user types in is to be stored in a variable named userInput of type int. You could do this with the assignment statement:userInput = EasyInput.getInt()
When the computer executes this statement, it will wait for the user to type in an integer value and then it will store that value in the variable userInput. (Recall, by the way, that the name "EasyInput.getInt" implies that the subroutine getInt is a part of the class EasyInput.) Here, for example, is a complete simple program that prints out the square of a number typed by the user:
public class PrintSquare { public static void main(String[] args) { // a program that computes and prints the square // of a number input by the user. int userInput; // the number input by the user int square; // the userInput, multiplied by itself System.out.print("Please type a number: "); userInput = EasyInput.getInt(); square = userInput * userInput; System.out.println("The square of that number is " + square); } // end of main() } //end of class PrintSquare
Even with EasyInput, I don't find standard Java input and output to be entirely satisfactory. I have written another class called Console that lets a program create what I call "console-style windows" to use for interaction with the user. It is this Console class that I will use for the remainder of this chapter and in the next chapter. The rest of this section discusses the Console class.
To use a console in your program, you must do two things. First, you must make sure that the Console class is available to your program -- for example, by adding the source file Console.java to your programming project. Second, you must add a statement to your program to create an object belonging to the Console class. This is because my console-style subroutines -- which should properly be called methods in this object-oriented context -- belong to objects rather than to classes. You don't need to understand this or other details of how Console objects are created and used until you get to Chapter 4. (What you are getting here is a little preview of object-oriented programming.) For now, just accept that you can use console input and output in a program that has the form:
public class program-name { public static void main(String[] args) { Console console = new Console(); // create the console window statements console.close(); //close the console window } }The main subroutine begins with a statement that creates a Console object named console. (Remember that "Console" and "console" are two compleletly different identifiers, since upper and lower case letters are considered to be different in Java. So the computer doesn't confuse the object, console, with the class, Console. However, if you are confused by this, you can use a different name for the object. If you want to use a console window named fred, for example, you could say "Console fred = new Console()." Of course, fred is not a particularly good name for a console.) When the computer executes the first statement of the program, it opens a window on the screen. You can then use subroutines from the console object to print output in the window and to read input typed by the user in the window. At the end of the program, the statement "console.close()" calls a subroutine that removes the window from the screen.
Every object of type Console has methods called put and putln for outputting data to the window. Since these methods are part of the object, they are called using compound names that include the name of the object. Here are some examples:
console.put("The answer is "); console.putln(ans);Note that console is the name of the object. If you used a different name for the object, you would have to substitute that name. (For example, if the object is named fred, you would use the statement "fred.putln(ans)". You could even have several objects of type Console, with different names. The object name in the subroutine would then tell the computer which window you want to print to.)
You can say "console.put(x);" or "console.putln(x);" for any expression x whose value is of type byte, short, int, long, float, double, char, boolean, or String. The expression can be a constant, a variable, or even something more complicated such as 2*x+y. The difference between put and putln is that putln adds a carriage return at the end of the output. If you use put, the output that follows will be on the same line.
If you want to output a carriage return, with no other output, you can say "console.putln();", with nothing between the parentheses. Note that "console.putln(x)" is exactly equivalent to "console.put(x); console.putln();".
Finally, I will note briefly that you can also do output using "console.put(x,n);" and "console.putln(x,n);", where n is an integer-valued expression. The idea is that n is the number of characters that you want to output. If x takes up fewer than n characters, then the computer will add some spaces at the beginning to bring the total up to n. (If x already takes up more than n characters, there's not much that the computer can do. It will just have to print out more characters than you ask for!) This feature is useful, for example, when you are trying to output neat columns of numbers, and you know just how many characters you need in each column.
The Console class is a little more versital at doing output than is System.out. However, it's input that we really need it for.
For input, the Console class uses functions. A function is a subroutine that returns a value. A function call can be used as part of an expression; often, though, the value is simply assigned to a variable. Here are examples of using all the input functions defined for objects of type Console:
b = console.getByte(); // value read is a byte i = console.getShort(); // value read is a short j = console.getInt(); // value read is an int k = console.getLong(); // value read is an long x = console.getFloat(); // value read is a float y = console.getDouble(); // value read is a double a = console.getBoolean(); // value read is a boolean c = console.getChar(); // value read is a char w = console.getWord(); // value read is a String s = console.getln(); // value read is a StringFor these statements to be legal, the variables on the left side of each assignment statements must be declared to be the same type that is returned by the function on the right side.
When you call one of these functions, you are guaranteed that it will return a legal value of the correct type. If the user types in an illegal value as input -- for example, if you ask for a byte and the user types in a number that is outside the legal range of -128 to 127 -- then the computer will ask the user to re-enter the value.
You'll notice that there are two input functions that return Strings. The first, getWord(), returns a string consisting of non-blank characters only. When it is called, it skips over any spaces and carriage returns typed in by the user. Then it reads non-blank characters until it gets to the next space or carriage return. It returns all the non-blank characters that it has read in a String. The second input function, getln(), simply returns a string consisting of all the characters typed in by the user, including spaces, up to the next carriage return. The carriage return itself is not returned as part of the input string, but it is read and discarded by the computer. Note that the String returned by this function might be the empty string, "", which contains no characters at all.
The character input function getChar() simply reads and returns the next character typed by the user. This could be any character, including a space or a carriage return. If the user typed a carriage return, then the char returned by getChar() is the special linefeed character '\n'.
All the other input functions -- getByte(), getShort(), getInt(), getLong(), getFloat(), getDouble(), and getBoolean() -- will skip past any blanks and carriage returns that the user types. However, they will not skip past other characters. If you try to read two ints and the user types "2,3", the computer will read the first number correctly, but when it tries to read the second number, it will see the comma. It will regard this as an error and will force the user to retype the number. If you want to input several numbers from one line, you should make sure that the user knows to separate them with spaces, not commas.
The semantics of input is much more complicated than the semantics of output. The first time the program tries to read input from the user, the computer will wait while the user types in an entire line of input. The console stores that line in a chunk of internal memory called the input buffer. Input is actually read from the buffer, not directly from the user's typing. The user only gets to type when the buffer is empty. This lets you read several numbers from one line of input, but if you only want to read in one number and the user types in extra stuff on the line, then you will be in trouble. To help you avoid this, there are versions of the console input functions that read a data value and then discard any leftover stuff on the line:
b = console.getlnByte(); // value read is a byte i = console.getlnShort(); // value read is a short j = console.getlnInt(); // value read is an int k = console.getlnLong(); // value read is an long x = console.getlnFloat(); // value read is a float y = console.getlnDouble(); // value read is a double a = console.getlnBoolean(); // value read is a boolean c = console.getlnChar(); // value read is a char w = console.getlnWord(); // value read is a StringNote that calling getlnByte(), for example, is equivalent to first calling getByte() and then calling getln() to read any remaining data on the same line.
Using Console for input and output, we can now improve our program for computing the value of an investment. We can have the user type in the initial value of the investment and the interest rate. The result is a much more useful program -- our first example program that it makes sense to run more than once!
public class Interest3 { /* This class implements a simple program that will compute the amount of interest that is earned on an investment over a period of 5 years. The initial amount of the investment and the interest rate are input by the user. The value of the investment at the end of each year is output. */ public static void main(String[] args) { Console console = new Console(); double principal; // the value of the investment double rate; // the annual interest rate console.put("Enter the initial investment: "); principal = console.getlnDouble(); console.put("Enter the annual interest rate: "); rate = console.getlnDouble(); int years = 0; // counts the number of years that have passed while (years < 5) { double interest = principal * rate; // compute this year's interest principal = principal + interest; // add it to principal years = years + 1; // count the current year. if (years > 1) { console.put("The value of the investment after "); console.put(years); console.put(" years is $"); } else { console.put("The value of the investment after 1 year is $"); } console.putln(principal); } // end of while loop console.close(); } // end of main() } // end of class Interest3Try out the equivalent applet here:
[ Next Section | Previous Section | Chapter Index | Main Index ]