rewrote a program and it still isn't working right for the catch statement

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);
}
}
 
P

Patricia Shanahan

judith said:
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
....

You have at least two problems. For one of them, I've already given you
my best advice, and it still applies. Work through your program,
specifically a case that is going wrong, keeping written notes of the
value of each variable. Be very careful about any assumptions - you need
to do exactly what the program would have done.

You also need to take a closer look at the java.util.Scanner
documentation, especially what happens when it throws
InputMismatchException. Does it advance past the token that didn't
match? Do you want to go on processing that line?

As others have already suggested, you might be better off not using
exceptions to control your program flow.

Once you understand your problems you will probably see how to fix them,
but if not you will at least be able to ask more focused questions.

Patricia
 
S

steve

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);
}
}

you need to read about program flow, and what happends when an exception is
triggered.
Exceptions should NOT be used like this.
 
A

Andrew Thompson

Patricia said:
...

You have at least two problems. For one of them, I've already given you
my best advice, and it still applies. Work through your program,
specifically a case that is going wrong, keeping written notes of the
value of each variable. Be very careful about any assumptions - you need
to do exactly what the program would have done.
....
That is excellent advice (it was also excellent advice
when you first said it).

Performing a walk-through will clear a lot of the confusion.
Once you understand your problems you will probably see how to fix them,
but if not you will at least be able to ask more focused questions.

I agree completely. Further, I doubt that you are
likely to get many more useful or technical answers
to progress the problem, until you have made a
good attempt at that process.


Andrew T.
 
M

Mark Space

judith said:
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

In addition to Java, you also need to work on your English syntax. This
sort of stuff makes me SO not want to answer your questions.

Follow Patricia's advice. Hand execution is the answer here. This
program is very simple; if you can't work this one out, you're doomed.
So you might as well learn how to do it now.

I doubt we can teach you over the 'net. You'll have to go to the
computer lab and ask someone to teach you how to hand execute. Or sit
in your professor's office and ask for the same advice. If you aren't
in a class, you should be. This is too remedial for a quick question on
a newsgroup. I'm sorry if this isn't the answer you wanted, but it's
honestly my best reply for your problem. Good luck.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,717
Messages
2,569,382
Members
44,705
Latest member
BerniePele

Latest Threads

Top