![]() |
![]() Spring Semester 2005
|
Let's start with a program that reads one line from the user:
Here's how it runs:class One { public static void main(String[] args) { ConsoleReader c = new ConsoleReader(System.in); System.out.print("Type something: "); String user; user = c.readLine(); System.out.println("You have typed: " + user); } }
Now suppose that the user is going to actually enter numbers for me to sum up.frilled.cs.indiana.edu%java One Type something: whatever You have typed: whatever
Here's what I might do, to get those numbers from the user:
Then the program might actually work like this:class One { public static void main(String[] args) { ConsoleReader c = new ConsoleReader(System.in); System.out.print("Type something: "); String user; user = c.readLine(); int number; number = Integer.parseInt(user); System.out.println(number); } }
So if the user enters something that is not a number I crash.frilled.cs.indiana.edu%javac One.java frilled.cs.indiana.edu%java One Type something: 12 12 frilled.cs.indiana.edu%java One Type something: bye Exception in thread "main" java.lang.NumberFormatException: For input string: "bye" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) at java.lang.Integer.parseInt(Integer.java:468) at java.lang.Integer.parseInt(Integer.java:518) at One.main(One.java:9) frilled.cs.indiana.edu%
Let's assume the user will give us numbers all the way.
Then I can add them up, each one as follows:
(Exercise: think how the program behaves if you run it.)class One { public static void main(String[] args) { int balance; balance = 0; ConsoleReader c = new ConsoleReader(System.in); System.out.print("Type something: "); String user; user = c.readLine(); int number; number = Integer.parseInt(user); balance = balance + number; System.out.println("The current balance is: " + balance); // get ready to work on the next number System.out.print("Type something: "); user = c.readLine(); // let's not parse the number just yet... } }
Now suppose we want to acknowledge the user potentially saying "bye"
.
When that happens we need to not expect a number:
Now let's make one small change to the program:frilled.cs.indiana.edu%cat One.java class One { public static void main(String[] args) { int balance; balance = 0; ConsoleReader c = new ConsoleReader(System.in); System.out.print("Type something: "); String user; user = c.readLine(); int number; if (! user.equals("bye")) { number = Integer.parseInt(user); balance = balance + number; System.out.println("The current balance is: " + balance); System.out.print("Type something: "); user = c.readLine(); // we have the new line, we don't know if it's a number though } // notice the if statement } } frilled.cs.indiana.edu%javac One.java frilled.cs.indiana.edu%java One Type something: 12 The current balance is: 12 Type something: 34 frilled.cs.indiana.edu%java One Type something: bye frilled.cs.indiana.edu%
frilled.cs.indiana.edu%cat One.java
class One {
public static void main(String[] args) {
int balance;
balance = 0;
ConsoleReader c = new ConsoleReader(System.in);
System.out.print("Type something: ");
String user;
user = c.readLine();
int number;
while (! user.equals("bye")) {
number = Integer.parseInt(user);
balance = balance + number;
System.out.println("The current balance is: " + balance);
System.out.print("Type something: ");
user = c.readLine();
}
}
}
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
Type something: 12
The current balance is: 12
Type something: 34
The current balance is: 46
Type something: -50
The current balance is: -4
Type something: -100
The current balance is: -104
Type something: 126
The current balance is: 22
Type something: -19
The current balance is: 3
Type something: 25
The current balance is: 28
Type something: bye
frilled.cs.indiana.edu%
Do you see the change? Do you see the effect it has on the program?
That's one small step for mankind.
But a giant leap for us.
So we should spend some time thinking about it.
Fri Feb 11 10:08:02 EST 2005