Why am I getting this error of incompatible types

Error in code!

import java.util.Scanner;

import java.util.InputMismatchException;

public class exceptionTest {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int n1, n2;

        double r;

        boolean answer = false;

        while (!answer) {
            try {
                System.out.println("What is the value of n1?");

                n1 = scan.nextInt();

                System.out.println("What is the value of n2?");

                n2 = scan.nextInt();

                if (n2 == 0)

                    throw new DividebyZeroException();


                r = (double) n1 / n2;

                System.out.println("Answer: " + n1 + "/" + n2 + "=" + r);

                answer = true;

            } catch (DividebyZeroException dvz) {
                scan.nextLine();

                System.out.println("you have attempted to divide by zero! Please try again!");
            } catch (InputMismatchException text) {
                scan.nextLine();

                System.out.println("You have entered a character and not an integer!");
            }


        }

    }

}



public class DividebyZeroException extends Exception
{
    public DividebyZeroException()
    {
        super("Divison by Zero!");
    }

    public DividebyZeroException(String banner)
    {
        super(banner);
    }
}
Matt VilkAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jeffrey Dake Senior Director of TechnologyCommented:
Can you paste in the exact error message you are getting?   Are you getting this when you compile or when you run the program?
0
Matt VilkAuthor Commented:
This is the error(s).
Capture.PNG
0
Jeffrey Dake Senior Director of TechnologyCommented:
Everything looks fine when I compile that exception class.  Do you have the exception class in its own file, and what are you using to compile?  Are you using an editor of some sort, or are you compiling by hand?
0
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Jeffrey Dake Senior Director of TechnologyCommented:
I also assume you are extending java.lang.Exception and not some other Exception class from somewhere.  Is that correct?
0
Matt VilkAuthor Commented:
i have an exception class in its own file, intelliJ to compile.
0
Jeffrey Dake Senior Director of TechnologyCommented:
Have you double checked that it is extending java.lang.Exception.  Try putting the in explicitly to make sure the compiler is picking up the right class.

public class DivideByZeroException extends java.lang.Exception
{

   public DivideByZeroException()
    {
        super("Divison by Zero!");
    }

    public DivideByZeroException(String banner)
    {
        super(banner);
    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Matt VilkAuthor Commented:
Thanks that worked!

Thanks for the help!!!
0
krakatoaCommented:
To confirm what Jeff is saying, your code above compiles and runs as it should. There must be a bad reference somewhere.
0
CEHJCommented:
Please note, that although your code works, it doesn't really make sense to throw exceptions and catch them in the same method. Exceptions are not only about dealing with errors - they are about flow control and alternative code paths. You might just as well deal with the situation, and much more easily, by checking and then changing the flow. IOW an exception isn't needed here
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.