Yahia A. Seridi Yahia A. Seridi - 20 days ago 7
Java Question

incompatible types: int cannot be converted to java.lang.String

so in the 4th method I'm trying to determine the winner but it says that the int cannot be converted to java.lang.String
and yes i have to use different methods, and if you find anything else wrong with the code let me know , thank you

import java.util.Random;// importing the random class to generate random number
import javax.swing.JOptionPane;//importing the JOptionPane to use the pop up windows and menu
public class TiriBark
{
private static int ComputerWin =0; // 0 is for loss and 1 for win
private static int UserWins =0; // 0 is for loss and 1 for win
private static int tie=0; // if it's a tie the value will be 1
private static int Comp; // holds the value of the random number generated by the computer
private static int user; // holds users choice of rock papper or scissor
public static void main(String[] args) // main method
{
Computer();
user();
}
/**this method generated a random number between 1 and 3
*@return Comp
*/
public static int Computer()
{

Random rand = new Random();
int Comp = rand.nextInt(3)+1;
return Comp;}
/**this method asked the user to enter his choice of rock paper or scissor
*@return user
*/
public static int user (){
String User = JOptionPane.showInputDialog("Enter 1 for rock 2 for paper and 3 for scissor ");
int user = Integer.parseInt(User);
return user; }
/** this method calculates and determines the winner and if possible a tie
*@return ComputerWin
*@return UserWins
*@return tie
*/
public static String resutls() {
if ( user == Comp ) {
tie =1; }
if ( user==1 && Comp == 2){
ComputerWin=1; }
if ( user ==1 && Comp ==3){
UserWins=1;}
if ( user ==2 && Comp ==1 ){
UserWins=1;}
if ( user ==2 && Comp == 3 ){
ComputerWin=1;}

if ( user ==3 && Comp ==1) {
ComputerWin=1; }

if ( user ==3 && Comp ==2 ){
UserWins=1;}
return UserWins;
return ComputerWin;
return tie;

}
}

Answer

Writing this:

return UserWins;
return ComputerWin;
return tie;

you are generating dead code, because the function ends at the first return. If you want to cast the int variable into String just do:

return UserWins + "";
Comments