-5

This is my second week in a Java course, so bear with me. I am trying to make a program that allows for a user input of "rock","paper", or "scissors." I am sure there are more efficient ways than what I have done, and I thought I was actually making progress, but now I stuck.

My applicable knowledge to this program is limited to if and if-else statements as well as switch statements. I tried to get a user input for rock, paper, or scissors, convert that to a number, and compare that to a randomly generated number, with 0 representing rock, 1 representing paper, and 2 representing scissors.

import java.util.*;

public class RockPaperScissors
{
    public static void main(String[] args) {

        int computer;

       computer = (int)(Math.random() * 2 + 1);

       Scanner input = new Scanner(System.in);

       String player;
       System.out.println("Enter the word rock, paper, or scissors.");
       player = input.next();

    if (computer == 0) {       
           System.out.println("The computer chose rock.");
    }
    else if (computer == 1) {
           System.out.println("The compuer chose paper.");
    }
    else if (computer == 2) {
           System.out.println("The computer chose scissors.");
    }


    // personally starting from here is where I feel like I started to make mistakes.
    int rock = 0;
    int paper = 1;
    int scissors = 2;
    player = (int)player;

       switch (player) {
           case "r" : rock = 0;
           player = 0;
           break;
           case 'p' : paper = 1;
           player = 1;
           break;
           case 's' : scissors = 2;
           player = 2;
           break;
           default : System.out.println("Invalid input");
    }

    if (player == computer) {
          System.out.println("You tied") ;     
        }
    else if (player = 1 && computer = 0 ^ player = 2 && computer = 0) {
        System.out.println("You won");
        }
    else {
        System.out.println("You lost");
    }
}
}
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
ComStudent
  • 1
  • 1
  • 1
  • 1
    Why are you casting a string to an integer?! `player` is a string! – Andrew Li Feb 16 '17 at 02:37
  • @AndrewLi, he's making it complicated it lol. he could just use the integer to do the calculation and return it as string in a case – FreedomPride Feb 16 '17 at 02:38
  • You don't understand a fundamental idea. You can't store Strings (a reference type) and ints (a primitive) in the same variable! Java is strongly-typed, which means `player` can't store a string and an integer! Also double quotes are for strings and single quotes are for chars! – Andrew Li Feb 16 '17 at 02:44
  • Also, your else if uses =... use `==` for comparison... – Andrew Li Feb 16 '17 at 02:45
  • 2
    Possible duplicate of [Converting String to Int in Java?](http://stackoverflow.com/questions/5585779/converting-string-to-int-in-java) – MrPublic Feb 16 '17 at 02:51
  • `player = (int)player;` this is wrong. – msagala25 Feb 16 '17 at 02:59

1 Answers1

1

In Java, you can use Integer.parseInt() to convert a String to int.

  1. Integer.parseInt() Examples Example to convert a String “10” to an primitive int.

    String number = "10";
    
    int result = Integer.parseInt(number);
    
    System.out.println(result);
    

Output

10

2.

Integer.valueOf() Examples Alternatively, you can use Integer.valueOf(), it will returns an Integer object.

String number = "10";
Integer result = Integer.valueOf(number);
System.out.println(result);

Output

10

Note

In summary, parseInt(String) returns a primitive int, whereas valueOf(String) returns a new Integer() object.

FreedomPride
  • 1,098
  • 1
  • 7
  • 30
  • But the whole premise of converting a String to an int is just wrong! Look in the switch statement - they are trying to test if a *string* equals *another string*! Why convert to an integer in the first place...? The **real** problem here is that they are attempting to use `player` for more than it should be. They are trying to store integers and strings in the same variable. – Andrew Li Feb 16 '17 at 02:40
  • @AndrewLi, true. If it's me i'll use the equal() function but i'm just providing the answer based on his header title. – FreedomPride Feb 16 '17 at 02:42
  • It really doesn't matter what's in the title. An answer must address all the problems, and there a few major ones that this answer does not address. – Andrew Li Feb 16 '17 at 02:44