The instructer wrote it this way. He wrote everything except the catch
"judith" <jspurlock83@hotmail.com> wrote in message
news:1161019659.941282.204000@f16g2000cwb.googlegroups.com...
// Loop until there is no error
do
{
try
{ error = false;
System.out.println("How many numbers do you want to enter?");
n = keyboard.nextInt();
if (n <= 0 )
throw new Exception ("Number must be greater than 0.");
}
catch (Exception e)
{
String message = e.getMessage();
System.out.println(message);
error = true;
}
}
while (error);
You're using exceptions for control flow, which is generally considered
bad form. See http://java.sun.com/docs/books/tutorial/essential/exceptions/
This problem might be more manageable if you refactored it into several
smaller methods. If each method is very short, and does one thing, then it's
easier to see if the method contains errors, as opposed to one big method
which does lots of different things.
public static void main(String[] args) {
int numberOfNumbers = getNumberOfNumbers();
int sum = 0;
for (int i = 0; i < numberOfNumbers; i++) {
sum = sum + getIthNumber(i);
}
displaySumAverageAndOtherStatistics(sum, numberOfNumbers);
}
- Oliver