Spring Semester 2005


Lecture Notes Nine: Here we go loop de loop...

The Conversation

Let's start with a program that reads one line from the user:

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); 
        
    }
}
Here's how it runs:
frilled.cs.indiana.edu%java One
Type something: whatever
You have typed: whatever
Now suppose that the user is going to actually enter numbers for me to sum up.

Here's what I might do, to get those numbers from the user:

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); 
        
    }
}
Then the program might actually work like this:
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%
So if the user enters something that is not a number I crash.

Let's assume the user will give us numbers all the way.

Then I can add them up, each one as follows:

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...  

    }
}
(Exercise: think how the program behaves if you run it.)

Now suppose we want to acknowledge the user potentially saying "bye".

When that happens we need to not expect a number:

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%
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; 

	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.


Last updated by Adrian German for A201/A597/I210 on Fri Feb 11 10:08:02 EST 2005