-2

Instead of System.out.println for everything, I want to use JOptionPane. I don't know how to describe how my program is skipping code. If you could please take a look at this and help it go in order that would be highly appreciated.

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



//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;
  String holeChoice;

  Scanner input = new Scanner(System.in);


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

  for (;;) {
   try {
 holeChoice =   JOptionPane.showInputDialog("For hole 9, please enter 9. If you are on hole 18, please enter 18 ");

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

    } else if (holeChoice.equals(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 8+ shots");
        hole18 = input.nextInt();

    } else if (holeChoice.equals(18)) {
      System.out.println("Please enter a valid number");
      continue;
    }
    break;
} catch (InputMismatchException inputMismatchException) {
    System.err.printf("\nException: %s\n", inputMismatchException);
    System.out.println("Please enter a valid number.");
    input.next();
}
}


//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 was boogie.");

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

 } else if (hole18 >= 8) {
   System.out.println("You need some more practice at golf.");

 } else if (hole18 <= 0) {
   System.out.println("Please input a valid number.");
   input.next();

 }


   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.");

 }  else if (hole9 >= 6) {
   System.out.println("Your need some more practice at golf.");

  }

 try {
  Thread.sleep(3000);                
}     catch(InterruptedException ex) {
      Thread.currentThread().interrupt();
}

    System.out.println("Thank you for playing Java Golf. Please come      again");

   }

}

  • What do you want it to do? any buttons? – roeygol Jan 21 '15 at 22:48
  • Please describe what order it is actually executing in. I assume commented steps are what you're expecting – mswieboda Jan 21 '15 at 22:48
  • Can you be more specific? – geekybedouin Jan 21 '15 at 22:48
  • Right now it skips all the way to "Please enter valid number" when either 9 or 18 is input into the JOptionPane. No buttons, just the simplest Pane will do. I just want to learn something more than System.out.println – user4476009 Jan 21 '15 at 22:51
  • Thanks to all who commented so quickly! Still errors in code being skipped so I just left the first and last message in a Pane which is enough for this purpose. Thanks again, -G4nd4lf- – user4476009 Jan 21 '15 at 23:16
  • You also have two conditionals that expect the same result, thus one of them will never be evaluated. It's the second of `else if (holeChoice.equals(18)) {`. You don't need the `continue` here either because there's no other code to skip. Also, the `break` is outside all of your `if` blocks, which means it will always break your loop regardless of user choice. The loop should only break if the user enters something valid. – Ryan J Jan 22 '15 at 01:14

2 Answers2

2

JOptionPane.showInputDialog returns a String, you're comparing the result as if it was an Integer.

holeChoice =   JOptionPane.showInputDialog("For hole 9, please enter 9. If you are on hole 18, please enter 18 ");
//...
if (holeChoice.equals(9))  // this will always be false

If the user enters 9, comparing it to the int 9 will not work, you need to add quotes to the values you check.

Ex:

if (holeChoice.equals("9"))

Likewise, you could also try to parse the input and turn it to an int, but you might open up the door for invalid user input (to which you'd need to check)

try {
   Integer choice = Integer.valueOf(holeChoice);
   //...
   if (choice.equals(9)) { // works now...
}
catch (NumberFormatException ex) {
   // user didn't enter a number!
}
Ryan J
  • 8,275
  • 3
  • 25
  • 28
1

The returned value from JOptionPane.showInputDialog(..) are String so use:

 if (holeChoice.equals("9")) { ... }

Or either parse the String to Integer using:

Integer.parseInt(holeChoice);
roeygol
  • 4,908
  • 9
  • 51
  • 88