• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

do while loop.

 
ken zemaitis
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and why


doesn't work?

Julien
 
ken zemaitis
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic