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. (At least this is true in the current version of Java.) 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 has only very primitive facilities, and it is not even implemented in some Java programming environments.

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. I've written my own "Console class" to provide this support. An object of class Console is a window where the user can type input and the computer can type output. Subroutines -- which should properly be called "methods" in this object-oriented context -- are provided for inputting and outputting all the standard types, including the eight primitive types and the String type.

For now, you don't have to understand all the details of how Console objects are created and used. (Nevertheless, you are going to get a little preview of programming with objects here.) 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();  // open 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.) You could use any name for the Console object that you like. When the computer executes this statement, 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.

An 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. (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
         x = 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 String

For 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 one 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
         x = console.getlnBoolean(); // value read is a boolean
         c = console.getlnChar();    // value read is a char
         w = console.getlnWord();    // value read is a String

Note 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 Interest3
         

Try out the equivalent applet here:

Sorry, your browser doesn't
support Java.


[ Next Section | Previous Section | Chapter Index | Main Index ]