Results 1 to 7 of 7
  1. #1
    jtyler is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default 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.

    -ask for total numbers
    -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);
                }
            }
            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!");
        }
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    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. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    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. #4
    jtyler is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default

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

  5. #5
    jtyler is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default

    NVM, got it.

    I mixed up 2 words.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,812
    Rep Power
    25

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

Similar Threads

  1. Exception Handling
    By liljester in forum New To Java
    Replies: 4
    Last Post: 06-21-2010, 04:09 PM
  2. Exception Handling help
    By MZA in forum New To Java
    Replies: 3
    Last Post: 02-10-2010, 10:23 AM
  3. Exception Handling...
    By focus_nitin in forum New To Java
    Replies: 1
    Last Post: 02-16-2008, 04:13 AM
  4. JDBC - Exception handling
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-05-2007, 05:00 PM
  5. JSTL Exception Handling
    By chaatf in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 07-18-2007, 03:24 AM

Posting Permissions

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