Re: i'm a new java user i'm having problems with my program using the try and catch
judith wrote:
Dear someone who can help
This program is supposed to calculate the average of N integers. The
program should prompt the user to enter the value for N and then
afterword ,ist emter all N numbers. If the user enters a non- positive
value for N then an exception should be thrown (and caught) with the
message "N must be positive. If there is any exception as the user is
entering the N numbers an error message should be displayed and the
user prompted to enter the number again.
The problem that i'm having is the followingMicrosoft Windows XP
[Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\Documents and Settings\judith spurlock>path=c:\Program
Files\Java\jdk1.5.0_01
\bin
C:\Documents and Settings\judith spurlock>cd\
C:\>java program3JS
How many numbers do you want to enter?
4
Enter number 1
-2
Enter number 2
-3
Enter number 3
4
Enter number 4
-3
The average is -1.0
C:\>java program3JS
How many numbers do you want to enter?
3
Enter number 1
1
Enter number 2
t
Error, please enter the number again
C:\>Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\Documents and Settings\judith spurlock>path=c:\Program
Files\Java\jdk1.5.0_01
\bin
C:\Documents and Settings\judith spurlock>cd\
C:\>java program3JS
How many numbers do you want to enter?
4
Enter number 1
-2
Enter number 2
-3
Enter number 3
4
Enter number 4
-3
The average is -1.0
C:\>java program3JS
How many numbers do you want to enter?
3
Enter number 1
1
Enter number 2
t
Error, please enter the number again
C:\>
when i enter a negative number it's supposed to output Number must be
greater than 0, instead it adds them up and gives an average. Also when
i enter anything other than a number like a letter it outputs Error,
please enter the number again but it exits instead of allowing me to
enter another number. here is the following program. I'm confused and
don't know how to fix it . could anyone suggest some help we have to
use the do while loop and the try and catch as per our instructer
Thanks judith
// Author: Judith Spurlock
// Course: ITSE 2417
// Program No: 3
// Due Date: 10/20/2006
//
// Program Name: program3JS.java
import java.util.Scanner;
import java.util.InputMismatchException;
public class program3JS
{
public static void main (String[] args)
{
// Variable declarations
boolean error = true;
double average;
int sum = 0;
int n = 0;
Scanner keyboard = new Scanner(System.in);
// Loop until there is no error
do
{
try
{
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.");
error = false;
}
catch (Exception e)
{
String message = e.getMessage();
System.out.println(message);
}
}
while (error);
// Loop through each number and calculate the average
int i;
for (i = 0; i < n; i ++)
{
// Repeat input as long as there is an error
do
{
try
{
System.out.println("Enter number " + (i+1));
int num = keyboard.nextInt();
sum += num;
error = false;
}
catch(InputMismatchException e)
{
System.out.println("Error, please enter the number again");
}
}
while (error);
}
average = sum/i;
System.out.println ("The average is " + average);
}
}