# Average with Exception Handling

• 10-04-2010, 01:25 AM
jtyler
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 :).

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);             }         }         catch (BadInput e)         {             String message = e.getMessage();             System.out.println(message);             anotherChance();         }     }     public static void anotherChance()     {         System.out.println("Enter N");         n = keyboard.nextInt();     }     public static void BadInput()     {         super("BAD INPUT; must be positive!");     } }```
• 10-04-2010, 02:14 AM
pbrockway2
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:

Code:

```int processed = 0 while(processed<total) {     // get number     // if good: update sum and average.  and increment processed }```
• 10-04-2010, 02:17 AM
pbrockway2
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.
• 10-04-2010, 02:32 AM
jtyler
Still am a little confused on the getMessage and BadException errors I'm getting.

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);         }         catch (BadInput e)         {             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();     }     private static class BadInput {         public BadInput(String string) {         }     } }```
• 10-04-2010, 04:07 AM
jtyler
NVM, got it.

I mixed up 2 words.
• 10-04-2010, 02:02 PM
Norm
• 10-04-2010, 11:27 PM
pbrockway2
Thanks, Norm.