-3

AND YES I have looked at simialr quesions and NO I cannot find an answer to my question...If you have a question about my question or code. PLEASE ASK.

do{
         try {
            System.out.print ("Volume of a cone... V = 1/3(3.14)r^2(h)");
            System.out.println ();
            System.out.print ("Input Radius: ");
            radius = keyBoard.nextDouble ();
            System.out.print ("Input Height: ");
            height = keyBoard.nextDouble ();
         //math
            volume = 0.333 * pie * radius * radius * height;
            System.out.printf ("Volume =   " + volume);
         }//end try
         catch (Exception Error){
            System.out.println ("You Entered the Wrong Data.");
         }
         finally {

            System.out.println ();

            System.out.print ("Do you want to try again?");
            System.out.println ();
            System.out.print ("Input '1' to go again OR any other key to End.: ");
            counter = keyBoard.nextInt ();
         }//end finally

      }while (counter == 1);
user2755775
  • 29
  • 1
  • 7
  • What is the question and does it even compile.Immediately downvoted :P – Kumar Abhinav Aug 30 '14 at 04:56
  • THIS is the output I Get WITH the ERROR Volume of a cone... V = 1/3(3.14)r^2(h) Input Radius: s You Entered the Wrong Data. Do you want to try again? Input '1' to go again OR any other key to End.: Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:909) at java.util.Scanner.next(Scanner.java:1530) at java.util.Scanner.nextInt(Scanner.java:2160) at java.util.Scanner.nextInt(Scanner.java:2119) at volumeConeC.main(volumeConeC.java:43) – user2755775 Aug 30 '14 at 04:57
  • This complies but once I input a 'Wrong Data' it immediately gives my exception in thread... – user2755775 Aug 30 '14 at 04:59
  • Bomb proof input is kinda hard. What level are you? You might want to ask your instructor what you can do here, they might give you some code if the input verification isn't part of the assignment. – markspace Aug 30 '14 at 04:59
  • I don't have an instructor to ask. – user2755775 Aug 30 '14 at 05:02

1 Answers1

0

problem:

counter = keyBoard.nextInt ();

After you input a string to the nextDouble it will consume the newLine character by the nextInt() and will result to InputMismatchException because newLine is not an Int.

solution:

consume the newLine character in your catch block before asking for input from the user

sample:

         catch (Exception Error){
            System.out.println ("You Entered the Wrong Data.");
            keyBoard.nextLine();
         }

EDIT:

boolean tryAgain = false;
    do{
         try {

             if(tryAgain)
             {
                System.out.println ();

                System.out.print ("Do you want to try again?");
                System.out.println ();
                System.out.print ("Input '1' to go again OR any other key to End.: ");
                counter = keyBoard.nextInt ();
                tryAgain = false;
             }

            System.out.print ("Volume of a cone... V = 1/3(3.14)r^2(h)");
            System.out.println ();
            System.out.print ("Input Radius: ");
            radius = keyBoard.nextDouble ();
            System.out.print ("Input Height: ");
            height = keyBoard.nextDouble ();
         //math
            volume = 0.333 * pie * radius * radius * height;
            System.out.printf ("Volume =   " + volume);
            tryAgain = true;
         }//end try
         catch (Exception Error){
            System.out.println ("You Entered the Wrong Data.");
            keyBoard.nextLine();
            tryAgain = true;
         }

      }while (counter == 1);
Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
  • THANK YOU. I seriously appreciate it. – user2755775 Aug 30 '14 at 05:04
  • @user2755775 click the check mark on this post to mark as answered. You are welcome and happy coding. – Rod_Algonquin Aug 30 '14 at 05:04
  • Rod_Algonquin...I have another somewhat unrelated question if you can answer it for me....Why is it that if I place the end of finally after the while the alignment of the blocks get messed up? – user2755775 Aug 30 '14 at 05:09
  • @user2755775 you can not put it outside the while statement. avoid doing that. – Rod_Algonquin Aug 30 '14 at 05:11
  • OP - did this solution work for you? I have some real doubts about it; in fact, I believe it's wrong. But you have given it the "green tick" which indicates it's OK. You've got this running then, or do you need more help? – Dawood ibn Kareem Aug 30 '14 at 05:13
  • @user2755775 Many people recommend not using the nextInt/nextDouble and similar for this reason. Consider nextLine instead followed with Integer.parseInt instead. – user2864740 Aug 30 '14 at 05:15
  • @DavidWallace I actually tried his code and input an **s** just like he did in the comment. – Rod_Algonquin Aug 30 '14 at 05:15
  • yes this works and I realize now that a finally wasn't even needed but I don't understand why it can't be placed outside the while – user2755775 Aug 30 '14 at 05:20
  • @Rod_Algonquin Kudos for actually testing your answer. But a newline character doesn't make `nextInt` throw an `InputMismatchException`. It's the `s` that causes the problem. So this might work, but not for the reason you gave. It may also give problems later if the user types something other than a number when they're asked whether they want to try again. So this really isn't a very robust solution, and the explanation is very misleading. I'd hate to think that the "green tick" that you pressured the OP into applying is going to discourage some other respondent from posting a better answer. – Dawood ibn Kareem Aug 30 '14 at 05:22
  • @David Wallace I see what you are saying about what is the problem. I didn't quite know how to word my question. For what I needed this worked but to make it better would be to replace counter.keyBoard.next() with nextLine?? – user2755775 Aug 30 '14 at 05:27
  • @DavidWallace he can always change hes code to add flags and still preserving the nextLine(). – Rod_Algonquin Aug 30 '14 at 05:31
  • @user2755775 you can always use flag see EDIT above. – Rod_Algonquin Aug 30 '14 at 05:31
  • That works well but it automatically restarts if everything is entered in correctly the first time around. – user2755775 Aug 30 '14 at 05:40
  • @user2755775 just add `tryAgain = true;` after the `System.out.printf ("Volume = " + volume);` – Rod_Algonquin Aug 30 '14 at 05:42
  • 1
    You cannot put the finally outside the while loop because, like the catch, the finally is part of the try block. It's there to make sure a certain set of code always gets executed even if an error occured. Putting the finally outside the while would be like taking an else and putting it outside the loop. – shieldgenerator7 Aug 30 '14 at 05:49