This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes do while loop. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "do while loop." Watch "do while loop." New topic
Author

do while loop.

ken zemaitis
Ranch Hand

Joined: Sep 26, 2005
Posts: 42
i am a total novice when it comes to java.
im having trouble completing this while statement.
the program is supposed to compile and run and be able to add multiple numbers together until a zero is entered.
any help anyone could provide would be much appreciated.
thanks a lot.

import java.io.*;
public class Lab11C {
public static void main(String[] args) {
int number = 0;
int total = 0;
do {
System.out.print("Enter a number or 0 to quit: ");
number = Utility.readInt();
System.out.println("You entered: " + number);
if (number != 0) {
total += number;
System.out.println("Total is now " + total);
}
} while(im not sure what to put here, ive tried just about everything i can think of.)
System.out.println("Processing complete");
}
}
class Utility {
public static int readInt() {
BufferedReader kb = new BufferedReader(new InputStreamReader(System.in));
Integer result = null;
while (result == null) {
try {
result = new Integer(kb.readLine());
}
catch(Exception err) {
System.out.print("ERROR! Please try again: ");
}
}
return result.intValue();
}
}
Julien Grenier
Ranch Hand

Joined: Sep 01, 2005
Posts: 41
and why


doesn't work?

Julien
ken zemaitis
Ranch Hand

Joined: Sep 26, 2005
Posts: 42
Originally posted by Julien Grenier:
and why


doesn't work?

Julien


wow, thought i tried that a few times, but apparently i had the wrong syntax. thanks a lot for the help.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: do while loop.
 
Similar Threads
Random Numbers and User Input help needed
endless loop
Must be caught or declared? Exceptions
Phonebook
StackOverflowError