Jump to content

Help with inputmismatchexceptions (java newb)

Here is my code 

public static void main(String[] args){ //execute the previously defined methods to calculate the quadratic equation.        while (cont){            System.out.println("What is the A value?"); //Asking the user for the A value in their quadratic equation.            Scanner Ascanner = new Scanner(System.in); //Created a new scanner variable for the A value            boolean validInput = false;            while(!validInput){            try {                double a = Ascanner.nextInt(); //Storing the next integer of that scanner variable for an Integer of the A value.System.out.println("What is the B value?"); //Asking the user for the B value in their quadratic equation.                validInput = true;            } catch (InputMismatchException e) {                System.out.println("That was not a number, please enter a numeric value");            }        }            double b = Ascanner.nextInt(); //Storing the next integer of that scanner variable for an Integer for the B value.            System.out.println("What is the C value?"); //Asking the user for the C value in their quadratic equation.            double c = Ascanner.nextInt(); //Storing the next integer of that scanner variable for an Integer of the C value.        //Finding the radicand        double tem1 = handleradicand(a,b,c);

neaer the beginning you can see that I am trying to get an A value(this is a program for calculating the quadratic equation, I have methods and stuff before this, but this is all that's necessary for help).  A scanner is then defined called Ascanner.  This is where stuff gets confusing.  Before I added this, everything was fine for the most part.  If you ignore the while loop and the try catch stuff, the user would input an integer and it would get stored as a.  But if the user input a letter or something else, it would crash, so i want to add something to handle that case, so that if the user inputs anything other than and integer, which would otherwise cause a runtime error, the program would prompt the user to try again, saying that was not a number, please enter a numeric value.

What happens, is that when i set the double a to the next integer, it doesn't get returned from this method, so that when i call it in handleradicand, at the bottom, it is undefined.  What should I do to either fix this, or maybe I should do something other than a try catch.

I can help with programming and hardware.

<Script>alert("This website is vulnerable to XSS");</Script>

Link to comment
Share on other sites

Link to post
Share on other sites

The problem is that you're creating the double a inside the loop. You need to initialize it outside of the loop:

            Scanner Ascanner = new Scanner(System.in); //Created a new scanner variable for the A value            boolean validInput = false;            double a = 0;            while(!validInput){            try {                a = Ascanner.nextInt(); //Storing the next integer of that scanner variable for an Integer of the A value.System.out.println("What is the B value?"); //Asking the user for the B value in their quadratic equation.                validInput = true;            } catch (Exception e) {                System.out.println("That was not a number, please enter a numeric value");            }        }

For stuff like this, you should put in print statements to help you locate the hiccups.

 

Just a note though: As it is, your try-catch statement is kind of useless since it only tests the first integer.

I own and use, sorted from newest to oldest: SteelSeries 6Gv2. Microsoft SideWinder X4. Mionix Naos 7000. Zowie EC1 Evo. Microsoft SideWinder X8. Microsoft IntelliMouse Explorer 3.0. Dell U2414H. Samsung P2270H. AKG K273 Pro. Sennheiser HD555. Razer Goliathus Speed Medium. Func 1030 L. Qpad CT Medium.

I used to own: Razer DeathAdder 3G. Razer Krait. IntelliMouse Optical 1.1. SteelSeries QcK.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×