简体   繁体   中英

Can't Figure Out How To Do a Try Catch Exception for my Java Program in Dr. Java?

So, I want Java to catch when the inputs something other than what's specified but I can't figure out how to do a proper try catch exception. Sometimes it just skips to the end of the program or in this case, I just get lines of error. If you can help that'd be great as I need to finish this program tomorrow. Sorry for the short notice and pressure.

//Step 1: Import Java APIs
import java.util.InputMismatchException;
import java.util.Scanner;
import java.io.*;




//Step 2: Name File and Class

public class GolfScores {



    //Step 3: Declare All Variables  

    public static void main(String[] args) {
       int hole9 = 0;
       int hole18 = 0;
       int holeChoice = 0;
       int albetross = 0;
       int eagle = 0;
       int birdie = 0;
       int par = 0;
       int boogie = 0;
       int boogie2 = 0;
       int holeinone = 0;
       int score = 0;
       int errorinput = 0;


       Scanner input = new Scanner(System.in);


       //Step 4: The program accepts INPUT from the user

       try {
           System.out.println("For hole 9, please enter 9. If you are on hole 18,          please enter 18 ");
           holeChoice = input.nextInt();

           if (holeChoice == 9) {
               System.out.println("The par for this hole is 3. Please enter if you this hole took you: 1, 2, 3, 4, 5, or more shots");
               hole9 = input.nextInt();

           } else if (holeChoice == 18) {
               System.out.println("The par for this hole is 5. Please enter if you this hole took you: 1, 2, 3, 4, 5, 6, 7, or more shots");
               hole18 = input.nextInt();
           }

       } catch ( InputMismatchException inputMismatchException)
       {
           System.err.printf ("\nException: %s\n",
                     inputMismatchException );

           System.out.println ("Please enter a valid number, either 9 or 18");
       }

       errorinput = input.nextInt();




      //Step 5 & 6: The user input is PROCESSED by Java and uses math to   calcualte an answer to output. The user's score is then output for them to see.
       if (hole18 == 1) {
           System.out.println("Your score for this hole was a hole in one!");

       } else if (hole18 == 2) {
           System.out.println("Your score for this hole was albetross.");

       } else if (hole18 == 3) {
           System.out.println("Your score for this hole was eagle.");

       } else if (hole18 == 4) {
           System.out.println("Your score for this hole was birdie.");

       } else if (hole18 == 5) {
           System.out.println("Your score for this hole was par.");

       } else if (hole18 == 6) {
           System.out.println("Your score for this hole waspboogie.");

       } else if (hole18 == 7) {
           System.out.println("Your score for this hole was double boogie.");

       } 


       if (hole9 == 1) {
           System.out.println("Your score for this hole was a hole in one!");

       } else if (hole9 == 2) {
           System.out.println("Your score for this hole was birdie.");

       } else if (hole9 == 3) {
           System.out.println("Your score for this hole was par.");

       } else if (hole9 == 4) {
           System.out.println("Your score for this hole was boogie.");

       } else if (hole9 == 5) {
           System.out.println("Your score for this hole was double boogie.");

       }

   }

}  

And here is the error:

Exception: java.util.InputMismatchException
Please enter a valid number, either 9 or 18
java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)


at java.util.Scanner.nextInt(Unknown Source)
at GolfScores.main(GolfScores.java:64)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at     edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

So it works in that it outputs the right statement, but how can I continue the program after this? I also need it for the rest of the inputs but if someone can show me how to do it for the first part, I think I can figure out the rest. Thank you.

Put your try-catch in a loop. Something like

for (;;) {
    try {
        System.out.println("For hole 9, please enter 9. "
                + "If you are on hole 18, please enter 18 ");
        holeChoice = input.nextInt();
        if (holeChoice == 9) {
            System.out.println("The par for this hole is 3. " //
                        + "Please enter if you this hole took you: "
                        + "1, 2, 3, 4, 5, or more shots");
            hole9 = input.nextInt();
        } else if (holeChoice == 18) {
            System.out.println("The par for this hole is 5. " //
                        + "Please enter if you this hole took you: "
                        + "1, 2, 3, 4, 5, or more shots");
            hole18 = input.nextInt();
        } else {
            System.out.println("Please enter a valid number, either 9 or 18");
            continue;
        }
        break;
    } catch (InputMismatchException inputMismatchException) {
        System.err.printf("\nException: %s\n", inputMismatchException)
        System.out.println("Please enter a valid number, either 9 or 18");
       input.next(); // consume bad token
    }
}

Your errorinput = input.nextInt(); is outside of try block. That is why you could not catch it. You may need another try-catch block here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM