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

Recommended Answers

All 11 Replies

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

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.

I just need a little help.

Please explain your problems or what you need help with.

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

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

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

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.

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.

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.