d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Help On Java Hw Rock Paper Scissors Program > Topic
12Next
Add Reply New Topic New Poll
Member
Posts: 2,092
Joined: Nov 22 2006
Gold: 433.69
Nov 24 2014 07:36pm
pm me please! Its beginner java
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 24 2014 07:42pm
Not doing PMs. but if you post your question and the relevant code, i might be able to help.
Member
Posts: 2,092
Joined: Nov 22 2006
Gold: 433.69
Nov 24 2014 07:45pm
Code
for some reason it gives me an error... i think its cuz of the int humanChoice=0 part.. i don't know how to fix it.

import java.util.Scanner;

public class RPS {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);


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

int cpuChoice = (int) (Math.random() * 3);
int humanChoice = 0;
System.out.println("Your hand" + humanChoice);
System.out.println("Computer hand:" + getCpuChoice(cpuChoice));
System.out.println("You" + didHumanWin(humanChoice, cpuChoice));

}


public static String getCpuChoice(int x) {
if (x == 0) {
return "Paper";

}

if (x == 1) {
return "Rock";

}
if (x == 2) {
return "Scissors";
}
return null;

}

public static String didHumanWin(int humanChoice, int cpuChoice) {
if (humanChoice == 0) {
if (cpuChoice != 2) {
if (cpuChoice != 0) {
return "Win";
}
return "Tie";
}
return "Lose";
}

if (humanChoice == 1) {
if (cpuChoice != 0) {
if (cpuChoice != 1) {
return "Win";
}
return "Tie";
}
return "Lose";
}

if (humanChoice == 2) {
if (cpuChoice != 1) {
if (cpuChoice != 2) {
return "Win";
}
return "Tie";
}
return "Lose";
}
return null;
}

public int getVal(String string) {
if (string.equalsIgnoreCase("paper")) {
return 0;
}
if (string.equalsIgnoreCase("rock")) {
return 1;
} else {
return 2;
}

}
}


This post was edited by cKranez on Nov 24 2014 07:50pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 24 2014 07:46pm
1) use [code] tags
2) post your error. if you know the line, highlight it
Member
Posts: 2,092
Joined: Nov 22 2006
Gold: 433.69
Nov 24 2014 07:49pm
sorry im new i dont know what you mean by 1) use [code]tags
2) theres no error in netbeans shown its only when i compile and then run it where it starts to give me an exception in CMD.

edit: figured the first part out lol.

This post was edited by cKranez on Nov 24 2014 07:50pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 24 2014 07:52pm
Quote (cKranez @ Nov 24 2014 08:49pm)
sorry im new i dont know what you mean by 1) use [code]tags
2) theres no error in netbeans shown its only when i compile and then run it where it starts to give me an exception in CMD.

edit: figured the first part out lol.


and what is that exception?
Member
Posts: 2,092
Joined: Nov 22 2006
Gold: 433.69
Nov 24 2014 07:58pm
sec



This post was edited by cKranez on Nov 24 2014 08:00pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 24 2014 08:02pm
Quote (cKranez @ Nov 24 2014 08:58pm)
sec

http://i.imgur.com/eUOzSHH.png


if you look at the error, it says InputMismatchException. lets review the documentation to see what this exception means:
https://docs.oracle.com/javase/7/docs/api/java/util/InputMismatchException.html
Quote
Thrown by a Scanner to indicate that the token retrieved does not match the pattern for the expected type, or that the token is out of range for the expected type.


looking further at your stacktrace, it tells you the error is on line 22 of your main method, when you call nextInt.

if you review the documentation for Scanner's nextInt() method, it states InputMismatchException can be thrown under certain scenarios:
https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextInt()

Quote
InputMismatchException - if the next token does not match the Integer regular expression, or is out of range


look at your input. you're entering a string "rock" and you're calling nextInt. given what i said above, what do you think the problem might be?

This post was edited by carteblanche on Nov 24 2014 08:09pm
Member
Posts: 2,092
Joined: Nov 22 2006
Gold: 433.69
Nov 24 2014 08:12pm
InputMismatchException - if the next token does not match the Integer regular expression, or is out of range

it does not match the integer regular expression? for line 22?


edit: so int can not be converted into a string. but how do i fix that?

This post was edited by cKranez on Nov 24 2014 08:16pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 24 2014 08:15pm
Quote (cKranez @ Nov 24 2014 09:12pm)
InputMismatchException - if the next token does not match the Integer regular expression, or is out of range

it does not match the integer regular expression? for line 22?

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


this line of code reads: "the user just entered an int. assign this value to the variable choice"

you entered "rock". how do you expect to convert "rock" to be an integer?

This post was edited by carteblanche on Nov 24 2014 08:15pm
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll