|
Main Article: Computer Science 124
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 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 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 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 Interest
{
/*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);
}
console.close();
}
}
|
IP: 38.107.191.102 |
Country: United States
|
Browser: Unknown |
OS: Unknown |
|
|