1,105,556 Community Members

Average with Exception Handling

Member Avatar
jtylerboy222
Newbie Poster
14 posts since Sep 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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!");
    }
}
Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

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

Member Avatar
jtylerboy222
Newbie Poster
14 posts since Sep 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

I just need a little help.

Please explain your problems or what you need help with.

Member Avatar
jtylerboy222
Newbie Poster
14 posts since Sep 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

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

Member Avatar
jtylerboy222
Newbie Poster
14 posts since Sep 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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
Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
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.

Member Avatar
jtylerboy222
Newbie Poster
14 posts since Sep 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

No

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
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.

Member Avatar
jtylerboy222
Newbie Poster
14 posts since Sep 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
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 three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article