# Thread: Average with Exception Handling

1. Member
Join Date
Sep 2010
Posts
5
Rep Power
0

## Average with Exception Handling

I believe all of my logic is sound, but syntax is off a bit...

It won't compile and I am not sure how to fix it.

-for loop to input total numbers
-1)input first number
-2)throw exception "must be positive"
-3)if negative > back to 1
-4)if no exception > calculate average

This is my first program using exception handling, so go easy :).

Java Code:
import java.util.Scanner;
public class Average {
public static void main (String[] args)
{
try
{
int n, total, sum, average;

Scanner keyboard = new Scanner(System.in);
System.out.println("Enter total numbers to be entered");
total = keyboard.nextInt( );

for (i=0;i<total;i++)
{
System.out.println("Enter N");
n = keyboard.nextInt( );

if (n<0)
throw new BadInput("N must be positive");

sum = n + sum;
average = sum/total;

System.out.println("Average is " + average);
}
}
{
String message = e.getMessage();
System.out.println(message);
anotherChance();

}
}
public static void anotherChance()
{
System.out.println("Enter N");
n = keyboard.nextInt();
}
{
}
}

2. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
14
What is the exact compiler message?

Note that BadInput must be a class defined somewhere. Your "public static void BadInput()" defines a method.

Why use exceptions to control the flow of your program? You could do something along the lines of:

Java Code:
int processed = 0
while(processed<total) {
// get number
// if good: update sum and average.  and increment processed
}

3. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
14
Do you really want to print the sum and average every time around the loop? If not, then just update sum inside the loop and move the calculation and display of average to after the loop has finished.

4. Member
Join Date
Sep 2010
Posts
5
Rep Power
0
Still am a little confused on the getMessage and BadException errors I'm getting.

Java Code:
import java.util.Scanner;

public class Average {
public static void main (String[] args)
{
try
{
int n, i, total, sum=0, average;

Scanner keyboard = new Scanner(System.in);
System.out.println("Enter total numbers to be entered");
total = keyboard.nextInt( );

for (i=0;i<total;i++)
{
System.out.println("Enter N");
n = keyboard.nextInt( );

if (n<0)
throw new BadInput("N must be positive");
}
sum = n + sum;
average = sum/total;

System.out.println("Average is " + average);
}
{
String message = e.getMessage();
System.out.println(message);
anotherChance();
}
}
public static void anotherChance()
{
int n;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter N");
n = keyboard.nextInt();
}
}
}
}

5. Member
Join Date
Sep 2010
Posts
5
Rep Power
0
NVM, got it.

I mixed up 2 words.

6. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
14

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•