4 Replies - 631 Views - Last Post: 07 November 2014 - 03:33 PM Rate Topic: -----

#1 cs1234  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 07-November 14

Error when compiling Rock Paper Scissors

Posted 07 November 2014 - 11:30 AM

When I try to compile the code it says " method determineWinner in class RPS cannot be applied to given types;"
I think there is something wrong with the way i called the determineWinner method but i'm not what exactly is wrong.


public class RPS
{

   public static void main( String [] args )
   {
      String result; 
      
     String userName = getUserName(); 
      String playerChoice, computerChoice;
    
     
      computerChoice = getComputerChoice();
      playerChoice = getPlayerChoice();
      
     String getResult = determineWinner(result);
      
		JOptionPane.showMessageDialog(null, "Compter Choice: " + computerChoice + "\n" + userName + "'s choice: " + playerChoice + "\n" + getResult);
      
         }
   
   public static String getUserName()
     {
          String enterName; 
      
          enterName = JOptionPane.showInputDialog("Enter your name:");
   
   return enterName; 
   }
   
   public static String getComputerChoice()
   {
      //Create a Random class object 
      Random randomNumbers = new Random();
      
      //
      String computerAssign; 
      
      //Generates a random integer from 1 to 3  
      int randomNum = randomNumbers.nextInt(3)+1;
   
      if 
      (randomNum ==1)
       computerAssign = "rock";
      
      else if   
      (randomNum == 2) 
      computerAssign = "paper";
      
      else
      computerAssign = "scissors";
      
      return computerAssign;
   }
   
   
   public static String getPlayerChoice ()
   {
            
      String playerAssign;
      do
      {
      playerAssign = JOptionPane.showInputDialog("Enter your choice of rock, paper, or scissors only:");
      } while (!(playerAssign.equalsIgnoreCase("rock") || playerAssign.equalsIgnoreCase("paper") || playerAssign.equalsIgnoreCase("scissors")));
            
      
     return playerAssign; 
     
     
    }
     
     
     public static String determineWinner (String player, String computer, String userName, String result)
   {
   		
   	if (player.equalsIgnoreCase("rock") && computer.equalsIgnoreCase("scissors")){
			result = (userName + " wins! Rock smash scissors.");    }    
                      
		if (player.equalsIgnoreCase("rock") && computer.equalsIgnoreCase("paper"))   {
			result = ("Computer wins! Paper wraps rock."); }
		
      if (player.equalsIgnoreCase("rock") && computer.equalsIgnoreCase("rock"))     {
			result= ("It's a tie! Game must be played again to determine the winner.");  }

		if (player.equalsIgnoreCase("paper") && computer.equalsIgnoreCase("scissor")) {
			result = ("Computer wins! Scissors cut paper."); }
         
		if (player.equalsIgnoreCase("paper") && computer.equalsIgnoreCase("paper")) {
			result = ("It's a tie! Game must be played again to determine the winner.");}
         
		if (player.equalsIgnoreCase("paper") && computer.equalsIgnoreCase("rock")) {
			result = (userName + " wins! Paper wraps rock.");} 
      
		if (player.equalsIgnoreCase("scissors") && computer.equalsIgnoreCase("scissors")) {
			result = ("It's a tie! Game must be played again to determine the winner.");}
         
		if (player.equalsIgnoreCase("scissors") && computer.equalsIgnoreCase("paper")) {
			result = (userName + " wins! Scissors cut paper.");}
      
		if (player.equalsIgnoreCase("scissors") && computer.equalsIgnoreCase("rock")) {
			result = ("Computer wins! Rock smashes scissors");}

         return result; 
      
		
	}
 
}

This post has been edited by modi123_1: 07 November 2014 - 11:53 AM
Reason for edit:: please use the 'code' button in the editor to apply the 'code' tags


Is This A Good Question/Topic? 0
  • +

Replies To: Error when compiling Rock Paper Scissors

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13568
  • View blog
  • Posts: 54,136
  • Joined: 12-June 08

Re: Error when compiling Rock Paper Scissors

Posted 07 November 2014 - 11:56 AM

Look at the method signature for "determineWinner":
072	     public static String determineWinner (String player, String computer, String userName, String result)



Now look how you are using it:
006	      String result; 
...
015	     String getResult = determineWinner(result);


Notice how you only use one variable in the code at row 015, but your method is looking for FOUR string parameters. As the error said - your use of "determineWinner" is wrong with your provided types. So pump in four strings to that puppy.
Was This Post Helpful? 1
  • +
  • -

#3 cs1234  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 07-November 14

Re: Error when compiling Rock Paper Scissors

Posted 07 November 2014 - 02:07 PM

Okay now it compiles however when I try to run the program. The result does not output. It is not reading it for some reason.








View Postmodi123_1, on 07 November 2014 - 11:56 AM, said:

Look at the method signature for "determineWinner":
072	     public static String determineWinner (String player, String computer, String userName, String result)



Now look how you are using it:
006	      String result; 
...
015	     String getResult = determineWinner(result);


Notice how you only use one variable in the code at row 015, but your method is looking for FOUR string parameters. As the error said - your use of "determineWinner" is wrong with your provided types. So pump in four strings to that puppy.

Was This Post Helpful? 0
  • +
  • -

#4 cs1234  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 07-November 14

Re: Error when compiling Rock Paper Scissors

Posted 07 November 2014 - 03:25 PM

When I try to run the program, it does not output the deterineWinner method in the JOptionPane message dialog beneath it. Is there something wrong with the way I called the method?

public class RPS
{

   public static void main( String [] args )
   {
     
     String userName = getUserName(); 
      
     String playerChoice, computerChoice;
     
     computerChoice = getComputerChoice();
     
     playerChoice = getPlayerChoice();
      
     String player = "";
     String computer = ""; 
     
     String getResults = determineWinner(player, computer,  userName);
      
     JOptionPane.showMessageDialog(null, "ROCK/PAPER/SCISSORS Game;" + "\n" + "\n" + "Game" + 
                                          "\n" + "\n" + "Compter Choice: " + computerChoice + 
                                          "\n" + userName + "'s choice: " + playerChoice + "\n" + getResults); 

                                          
         }
   
   public static String getUserName()
     {
          String enterName; 
      
          enterName = JOptionPane.showInputDialog("Enter your name:");
   
   return enterName; 
   }
   
   public static String getComputerChoice()
   {
      //Create a Random class object 
      Random randomNumbers = new Random();
      
      //
      String computerAssign; 
      
      //Generates a random integer from 1 to 3  
      int randomNum = randomNumbers.nextInt(3)+1;
   
      if 
      (randomNum ==1)
       computerAssign = "rock";
      
      else if   
      (randomNum == 2) 
      computerAssign = "paper";
      
      else
      computerAssign = "scissors";
      
      return computerAssign;
   }
   
   
   public static String getPlayerChoice ()
   {
            
      String playerAssign;
      do
      {
      playerAssign = JOptionPane.showInputDialog("Enter your choice of rock, paper, or scissors only:");
      } while (!(playerAssign.equalsIgnoreCase("rock") 
                 || playerAssign.equalsIgnoreCase("paper")  || playerAssign.equalsIgnoreCase("scissors")));

     return playerAssign; 
    }
     
     
     public static String determineWinner (String player, String computer, String userName)
   {
            
       String result = ""; 
           
       
   	if (player.equalsIgnoreCase("rock") && computer.equalsIgnoreCase("scissors")) {
         result = (userName + " wins! Rock smashes scissors"); }
                          
		else if (player.equalsIgnoreCase("rock") && computer.equalsIgnoreCase("paper"))  {
			result = ("Computer wins! Paper wraps rock."); }
		
      else if (player.equalsIgnoreCase("rock") && computer.equalsIgnoreCase("rock"))  {  
			result= ("It's a tie! Game must be played again to determine the winner."); }

		else if (player.equalsIgnoreCase("paper") && computer.equalsIgnoreCase("scissor")) {
			result = ("Computer wins! Scissors cut paper."); }
         
		else if (player.equalsIgnoreCase("paper") && computer.equalsIgnoreCase("paper")) {
			result = ("It's a tie! Game must be played again to determine the winner."); }
         
		else if (player.equalsIgnoreCase("paper") && computer.equalsIgnoreCase("rock")) {
			result = (userName + " wins! Paper wraps rock."); }
     
		else if (player.equalsIgnoreCase("scissors") && computer.equalsIgnoreCase("scissors")) {
			result = ("It's a tie! Game must be played again to determine the winner.");}
         
	   else if (player.equalsIgnoreCase("scissors") && computer.equalsIgnoreCase("paper")) {
			result = (userName + " wins! Scissors cut paper."); }
      
		else if  (player.equalsIgnoreCase("scissors") && computer.equalsIgnoreCase("rock")) {
			result = ("Computer wins! Rock smashes scissors"); }

           
		return (result);


This post has been edited by macosxnerd101: 07 November 2014 - 03:37 PM
Reason for edit:: Please use code tags

Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12189
  • View blog
  • Posts: 45,251
  • Joined: 27-December 08

Re: Error when compiling Rock Paper Scissors

Posted 07 November 2014 - 03:33 PM

1) Please do not duplicate post.

2) Please use code tags when posting code: :code:.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1