1.11M Members

Average with Exception Handling

 
0
 

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

-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 :).

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!");
    }
}
 
0
 

Sorry, I missed your question.
Does the program work ok?

 
0
 

No...I said I am guessing the syntax is wrong because it seems like my logic is right...
It's my first exception handling program and I just need a little help.

 
0
 

I just need a little help.

Please explain your problems or what you need help with.

 
0
 

The syntax is off
It won't compile
I don't know how to go about fixing it

 
0
 

Please copy and paste the full text of your error messages here if you want help fixing them.

 
0
 
Average.java:29: cannot find symbol
symbol  : class BadInput
location: class Average
                    throw new BadInput("N must be positive");
                              ^
Average.java:37: cannot find symbol
symbol  : class BadInput
location: class Average
        catch (BadInput e)
               ^
Average.java:48: cannot find symbol
symbol  : variable n
location: class Average
        n = keyboard.nextInt();
        ^
Average.java:48: cannot find symbol
symbol  : variable keyboard
location: class Average
        n = keyboard.nextInt();
            ^
Average.java:53: call to super must be first statement in constructor
        super("BAD INPUT; must be positive!");
             ^
5 errors
 
0
 

Which of these error message don't you understand.
The first two are because there is no class definition for BadInput.
The next is because the definition for n is local to a {} block and is out of scope for where it is being used. Move the definition for n out of the {}s so it is at the same level of {}s as where you are trying to use it.
The third one says: "call to super must be first statement in constructor"
Do you know what a constructor is?

Your title mentions Exception. That is a base class that many other classes extend. You need to create a class that extends the Exception class. Go read the API doc for the Exception class and see how many other classes extend it. There are dozens. Go look at a couple to see how they are used.

Do you know how to define a class?
Do you know how to define a class that extends another class?
Do you know how to write a constructor for a class? One that takes a parameter.

 
0
 

No

 
0
 

Do you have a textbook or online resource for java programming?
I suggest you do some research on these topics. They are very basic for writing a java program.

 
0
 

Thanks for the help bud.

I replaced 'BadInput' with 'Exception' and got rid of the method BadInput and it works.

You are doing a great job on here.

/sarcasm

 
0
 

Glad you figured it out. That is the goal.
Some OPs come here looking for someone to do their work for them.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article