0

I seem to be having issues with my code. I am attempting to create a Rock, Paper, Scissors game for my Java class. It requires this JOptionPane input type and also requires a try catch to handle exceptions. The input options are also a requirement. I can't find anything wrong with my code and I even added a System.out.println of the value in question to confirm it is the correct input when its passed to the if statement. The issue is that no matter what the input is, correct or incorrect, the exception for an incorrect input is always thrown. And yes I know running this ends up being and endless loop that can only be closed via terminating the process via Task Manager. Its just a snippet of all of the code.

Im using Eclipse Juno Build id: 20120614-1722 to compile and run my code on a Windows 7 64bit machine running JavaSE-1.6.

public static void main(String args[]){
    String input;
    int counter = 0;
    int inputnum;
    boolean valid = false;
    do
    {

        try {
            input = JOptionPane.showInputDialog("What is your choice?\n\nRock: (0,r,rock)\nPaper: (1,p,paper)\nScissors: (2,s,scissors)\nLizard: (3,l,lizard)\nSpock: (4,o,spock)");
            System.out.println(input);
            if (input == "0" || input == "r" || input =="rock"){
                inputnum = 1;
                valid = true;
                counter += 1;
            }
            else if (input == "1" || input == "p" || input == "paper"){
                inputnum = 2;
                valid = true;
                counter += 1;
            }
            else if (input == "2" || input == "s" || input == "scissors"){
                inputnum = 3;
                valid = true;
                counter += 1;
            }
            else if (input == "3" || input == "l" || input == "lizard"){
                inputnum = 4;
                valid = true;
                counter += 1;
            }
            else if (input == "4" || input == "o" || input == "spock"){
                inputnum = 5;
                valid = true;
                counter += 1;
            }
            else {
                throw new IllegalArgumentException("Invalid Input!");
            }
        }
        catch (IllegalArgumentException e){
            inputnum = 0;
            JOptionPane.showMessageDialog(null, "Invalid input! Try again!");
        }
        catch (Exception e){
            inputnum = 0;
            JOptionPane.showMessageDialog(null, "Unexpected Exception! Try again!");
        }





    } while (valid == false);

}
S019
  • 1

0 Answers0