J
judith
The problem that i'm having is when the program asks you How many
numbers do you want to enter and you put in a negative number it
outputs the correct response Number must be greater than 0 but it exits
into the c prompt with an error message. and also when it asks you to
enter a number and you enter a letter it's supposed to output Error
please enter the number again and it's supposed to allow you to do that
but it just keeps repeating itself. here is an example and the program
will follow
i don't know what to do Judith Can someone please help
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?
-1
Number must be greater than 0.
Exception in thread "main" java.lang.ArithmeticException: / by zero
at program3JS.main(program3JS.java:75)
C:\>java program3JS
How many numbers do you want to enter?
t
null
Exception in thread "main" java.lang.ArithmeticException: / by zero
at program3JS.main(program3JS.java:75)
C:\>java program3JS
How many numbers do you want to enter?
3
Enter number 1
t
Error, please enter the number again
Enter number 2
Error, please enter the number again
Enter number 3
Error, please enter the number again
The average is 0.0
C:\>error = false;
// 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
{ 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);
}
}
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
{ error = false;
System.out.println("Enter number " + (i+1));
int num = keyboard.nextInt();
sum += num;
}
catch(InputMismatchException e)
{
System.out.println("Error, please enter the number again");
}
}
while (error);
}
average = sum/i;
System.out.println ("The average is " + average);
}
}
numbers do you want to enter and you put in a negative number it
outputs the correct response Number must be greater than 0 but it exits
into the c prompt with an error message. and also when it asks you to
enter a number and you enter a letter it's supposed to output Error
please enter the number again and it's supposed to allow you to do that
but it just keeps repeating itself. here is an example and the program
will follow
i don't know what to do Judith Can someone please help
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?
-1
Number must be greater than 0.
Exception in thread "main" java.lang.ArithmeticException: / by zero
at program3JS.main(program3JS.java:75)
C:\>java program3JS
How many numbers do you want to enter?
t
null
Exception in thread "main" java.lang.ArithmeticException: / by zero
at program3JS.main(program3JS.java:75)
C:\>java program3JS
How many numbers do you want to enter?
3
Enter number 1
t
Error, please enter the number again
Enter number 2
Error, please enter the number again
Enter number 3
Error, please enter the number again
The average is 0.0
C:\>error = false;
// 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
{ 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);
}
}
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
{ error = false;
System.out.println("Enter number " + (i+1));
int num = keyboard.nextInt();
sum += num;
}
catch(InputMismatchException e)
{
System.out.println("Error, please enter the number again");
}
}
while (error);
}
average = sum/i;
System.out.println ("The average is " + average);
}
}