| Author |
need help converting string to int
|
Sara Bear
Greenhorn
Joined: Nov 05, 2012
Posts: 8
|
|
my errors are:
RockPaperScissors.java:30: error: method getWinner in class RockPaperScissors ca
nnot be applied to given types;
getWinner(player, computer);
^
required: int,int
found: String,int
reason: actual argument String cannot be converted to int by method invocation
conversion
RockPaperScissors.java:41: error: cannot find symbol
playerAnswer.toInt = 0;
^
symbol: variable playerAnswer
location: class RockPaperScissors
RockPaperScissors.java:43: error: cannot find symbol
playerAnswer = 1;
^
symbol: variable playerAnswer
location: class RockPaperScissors
RockPaperScissors.java:45: error: cannot find symbol
playerAnswer = 2;
^
symbol: variable playerAnswer
location: class RockPaperScissors
RockPaperScissors.java:46: error: cannot find symbol
return playerAnswer;
^
symbol: variable playerAnswer
location: class RockPaperScissors
RockPaperScissors.java:73: error: cannot find symbol
playerAnswer = playerAnswer(player);
^
symbol: variable player
location: class RockPaperScissors
6 errors
I kind of assumed it would look something like this:
public static int playerAnswer(String player)
{
Scanner Keyboard = new Scanner(System.in);
System.out.println("Choose rock, paper or scissors");
player = keyboard.nextLine(); //maybe player.toInt = keyboard.nextLine()
if (player.equalsIgnoreCase("rock"))
playerAnswer = 0; // or maybe something like player.toInt = 0; instead of having playerAnswer
else if(player.equalsIgnoreCase("paper"))
playerAnswer = 1;
else if (player.equalsIgnoreCase("scissors"))
playerAnswer = 2;
return playerAnswer;
}
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 5902
|
|
You're already converting a String to an int. You're just ignoring that int and passing your original String to the method that expects an int.
A couple of other points:
You should only create a single Random, rather than creating a new one each time through the method. Make it a static member varaible.There's no need to pass an int to your computerRandom() method. You're not using that parameter at all. Just declare a local variable instead.
|
 |
 |
|
|
subject: need help converting string to int
|
|
|