Jump to content

Converting Lowercase of Capital Letters

- - - - -

  • Please log in to reply
2 replies to this topic

#1
Guest_Intr3p1dN4t3_*

Guest_Intr3p1dN4t3_*
  • Guests
Hello!

I am a beginning Java programmer and am working on a program that plays Rock Paper Scissors with the user.

To find out whether the user wants rock, paper, or scissors, I ask they in a JOptionPane. My problem is that their answer is case-sensitive. I put in a toUpperCase() object but the program still gives me an error when I type a lowercase answer. What am I doing wrong? Thank you

public class RockPaperScissors {


    final static int ROCK = 0, SCISSORS = 1, PAPER = 2;


    public static void main(String args[]) {

        int z = 0;

        String user_choice, ComputerRockPaperScissors, play_again;

        Random m = new Random();

        do {

            user_choice = JOptionPane.showInputDialog("Rock, Paper, Scissors?");

            user_choice.toUpperCase();


            int comp_choice = m.nextInt(3);

            ComputerRockPaperScissors = convert(comp_choice);



            JOptionPane.showMessageDialog(null, "You chose " + user_choice + " and the computer chose " + ComputerRockPaperScissors + ". " + whoWon(user_choice, comp_choice));


            play_again = JOptionPane.showInputDialog("Would you like to play again?");

            play_again.toUpperCase();


        } while (play_again.equals("Yes"));

    }


    public static String whoWon(String user_input, int comp_choice) {

        int userRockPaperScissors = 9;

        String end = "ERROR";

        //convert String response to integer

        if (user_input.equals("Rock")) {

            userRockPaperScissors = ROCK;

        }

        if (user_input.equals("Scissors")) {

            userRockPaperScissors = SCISSORS;

        }

        if (user_input.equals("Paper")) {

            userRockPaperScissors = PAPER;

        }

        //get result

        if (((userRockPaperScissors == PAPER) && (comp_choice == SCISSORS))

                || ((userRockPaperScissors == ROCK) && (comp_choice == PAPER))

                || ((userRockPaperScissors == SCISSORS) && (comp_choice == ROCK))) {

            end = "You Lose";

        }

        if (((userRockPaperScissors == SCISSORS) && (comp_choice == PAPER))

                || ((userRockPaperScissors == PAPER) && (comp_choice == ROCK))

                || ((userRockPaperScissors == ROCK) && (comp_choice == SCISSORS))) {

            end = "You Win";

        }


        if (userRockPaperScissors == comp_choice) {

            end = "Tie!";

        }

        return end;



  }


    public static String convert(int v) {

        String random_choice = "Rock";

        if (v == 1) {

            random_choice = "Scissors";

        }

        if (v == 2) {

            random_choice = "Paper";

        }

        return random_choice;

    }

}


#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
toUpperCase() returns the uppercase string. It doesn't change the string you're using it on.
What you want is:
user_choice = user_choice.toUpperCase();
Alternatively you can use equalsIgnoreCase instead of equals.

#3
Guest_Intr3p1dN4t3_*

Guest_Intr3p1dN4t3_*
  • Guests
Thank you for the help!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users