|
Main Article: Computer Science 124
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 variableuserInput. (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);
}
}
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 Console. (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 ConsoleExample
{
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.
|
IP: 38.107.191.102 |
Country: United States
|
Browser: Unknown |
OS: Unknown |
|
|