• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

need help converting string to int

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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;
}
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
  •  
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic