File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes do while loop. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply 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.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: do while loop.
 
Similar Threads
Phonebook
endless loop
StackOverflowError
Random Numbers and User Input help needed
Must be caught or declared? Exceptions