d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Help On Java Hw Rock Paper Scissors Program > Topic
Prev12
Add Reply New Topic New Poll
Member
Posts: 2,092
Joined: Nov 22 2006
Gold: 433.69
Nov 24 2014 08:17pm
but i thought i assigned a value already to rock? at the bottom.

what would i have to change int to? to fix it?

This post was edited by cKranez on Nov 24 2014 08:19pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 24 2014 08:20pm
Quote (cKranez @ Nov 24 2014 09:17pm)
but i thought i assigned a value already to rock? at the bottom.


are you referring to the method getVal? Lets look at your code again:

Code
System.out.println("Enter your hand:");
int choice = input.nextInt();


can you show me where in those 2 lines of code you call getVal? i'm not seeing it.
Member
Posts: 2,092
Joined: Nov 22 2006
Gold: 433.69
Nov 24 2014 08:22pm
Code
public int getVal(String string) {
if (string.equalsIgnoreCase("paper")) {
return 0;
}
if (string.equalsIgnoreCase("rock")) {
return 1;
} else {
return 2;
}

}


at the bottom.. i thought this establishes the values.
Member
Posts: 2,092
Joined: Nov 22 2006
Gold: 433.69
Nov 24 2014 08:29pm
ok i switched it out to String choice = input.nextLine(); seems to work so far

i didnt really understadn the part where u made it clear that rock is not an int value

This post was edited by cKranez on Nov 24 2014 08:30pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 24 2014 08:32pm
Quote (cKranez @ Nov 24 2014 09:29pm)
i didnt really understadn the part where u made it clear that rock is not an int value


getVal is simply a method that converts your string to an int. methods are absolutely useless if you never use them. if you want to convert strings to an int, you have to call your method. i dont see you calling it anywhere. where do you think you should call it?
Member
Posts: 2,092
Joined: Nov 22 2006
Gold: 433.69
Nov 24 2014 08:35pm
Honestly, we just learned about methods and i do not know or understand it too well and my text book doesnt help either sorry :T
Member
Posts: 23,337
Joined: Nov 2 2006
Gold: 7,528.30
Nov 24 2014 10:39pm
If you input a a string "rock", it will try to store it into int "choice" as you have written. That is the error. Your method setValue or whatever at the bottom is never called. the syntax for calling a method is:

methodToBeCalled(value[s])

or what you should have wrote:

int choice = getVal(input.getInt());

OR

String choice = input.getInt();
int numberChoice = getVal(choice);

you really don't even need the function even, since you could just be using comparisons like if( humanChoice.equals("rock") && computerChoice.equals("scissors")return true;
and then you'd just have the math.Random method do if(roll = 0) computerChoice = "Rock"; or something

This post was edited by ForTheMathematics on Nov 24 2014 10:40pm
Go Back To Programming & Development Topic List
Prev12
Add Reply New Topic New Poll