Jump to content

Java: OOP "cannot be applied to given types"

Beeeyeee

Herro! I'm work on Object Oriented Programming.

My teacher wrote one side (RPSGameGUI.java) and I'm writing (RockPaperScissors.java) the goal is to make a simulated game of Rock, Paper, Scissors. Pretty simple, right? sorry if the code is a little messy, I tried my best!

 

Basically my issue is this error:


 "RPSGameGUI.java:52: error: method pickWinner in class RockPaperScissors cannot be applied to given types;
            winner = rps.pickWinner(userChoice, cpuChoice);  //***Your method
                        ^
  required: String,String,String,String
  found: String,String
  reason: actual and formal argument lists differ in length
1 error"

 

As far as I can tell, the RPSGameGUI.java is using this line of code:

 winner = rps.pickWinner(userChoice, cpuChoice);  //***Your method 

to pull String userChoice & String cpuChoice from MY method back into this main method. Is that thinking correct?

In my pickWinner method, I'm only returning String winner. The error seems to indicate it wants four Strings, when I'm only giving it two... But I'm only returning one String so this is where i'm confused.

Am I passing String cpuChoice and String userChoice back into the main method or what? 

 

Thanks for any help! 

 

Main Method:


import javax.swing.JOptionPane;

public class RPSGameGUI
{

    public static void main (String[] args)
    {
        //Creating the RockPaperScissors object
      RockPaperScissors rps = new RockPaperScissors ();  //***Your class
                
        // Create variables to hold data
      int numGames = 0;
        String userChoice = "";
        String cpuChoice = "";
        String winner = "";
        int userWins;
        int cpuWins;
      String playerName = "";
      int playAgain = 0;
      
      //Show input box
      playerName = JOptionPane.showInputDialog(null, "Please enter your name: ");
        
        //Show message box
      JOptionPane.showMessageDialog(null, "Welcome " + playerName);
        
      do
      {
        //Set wins to 0 at beginning of loop.  Will reset to 0 if loop executes again.
      userWins = 0;
      cpuWins = 0;
      
      //Show input box and parse to integer
      numGames = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter an odd number of games to play: "));
        
        //Validate input
      while (numGames % 2 == 0 || numGames < 1) //Even number or less than 1
        {
            numGames = Integer.parseInt(JOptionPane.showInputDialog(null, "Sorry, number of games must be greater than 1 and odd.  Please try again:"));
        }
        
        //Play the game for the number of rounds the user entered
        for (int i = 1; i <= numGames; i++)
        {
            //Get the user and computer choices
            userChoice = rps.getUserChoice();  //***Your method
            cpuChoice = rps.getCPUChoice();   //***Your method
            
            JOptionPane.showMessageDialog(null, "Computer chooses " + cpuChoice);
            
            //Pick winner
            winner = rps.pickWinner(userChoice, cpuChoice);  //***Your method
            
            if (winner.equalsIgnoreCase("Tie"))
            {
                JOptionPane.showMessageDialog(null, "It's a tie!  Play again.");
                numGames++;
            }
            else
            {
                if (winner.equalsIgnoreCase("User"))
                {
                    userWins++;
                }
                else if (winner.equalsIgnoreCase("Computer"))
                {
                    cpuWins++;
                }
                else
                {
                    JOptionPane.showMessageDialog(null,"Error in picking winner");
                }
                
                JOptionPane.showMessageDialog(null, winner + " wins!");
            }
            
        } //end for
        
        //Print results
        JOptionPane.showMessageDialog(null, playerName + " won " + userWins + " time(s).");
        JOptionPane.showMessageDialog(null, "Computer won " + cpuWins + " time(s).");
        
        if (userWins > cpuWins)
        {
            JOptionPane.showMessageDialog(null, playerName + " won!");
        }
        if (cpuWins > userWins)
        {
            JOptionPane.showMessageDialog(null, "The computer won!");
        }
        
        //Show confirmation box
      playAgain = JOptionPane.showConfirmDialog(null, "\nThank you for playing! \n Would you like to play again?");
    
   } while(playAgain == JOptionPane.YES_OPTION);
      
    } //end main

} //end class

 

My methods:


 import java.util.Random;
 import javax.swing.JOptionPane;
 
 public class RockPaperScissors
 {
   //get user's choice
   public String getUserChoice()
   {
      String userChoice = "";
    
      userChoice = JOptionPane.showInputDialog(null, ("please choose Rock, Paper, or Scissors."));
     
      return userChoice;
      
   }//end userChoice
   
   //get cpu's choice
   public String getCPUChoice()
   {      
      Random r = new Random();
      int cpu = 0;
      String cpuChoice = "";
      
      cpu = r.nextInt(3)+1;
      
      if (cpu == 1)
         {
            cpuChoice = ("Rock");
         }
         else if (cpu == 2)
         {
            cpuChoice = ("Paper");
         }
         else if (cpu == 3)
         {
            cpuChoice = ("Scissors");
         }
      
      return cpuChoice;
      
   }//end cpuChoice
   
   //select winner
   public String pickWinner(String userChoice, String cpuChoice, String winner, String playerName)
   {      
      if ((cpuChoice.equals("Rock") && userChoice.equalsIgnoreCase("paper")) ||
          (cpuChoice.equals("Paper") && userChoice.equalsIgnoreCase("Scissors")) ||
          (cpuChoice.equals("Scissors") && userChoice.equalsIgnoreCase("rock")))
            {  
            winner = "User";
            }
      else if ((userChoice.equalsIgnoreCase("rock") && cpuChoice.equals("Paper")) ||
               (userChoice.equalsIgnoreCase("paper") && cpuChoice.equals("Scissors")) ||
               (userChoice.equalsIgnoreCase("scissors") && cpuChoice.equals("Rock")))
            {
            winner = "Computer";
            }
      else if ((userChoice.equalsIgnoreCase("Rock") && cpuChoice.equals("Rock")) ||
               (userChoice.equalsIgnoreCase("Paper") && cpuChoice.equals("Paper")) ||
               (userChoice.equalsIgnoreCase("Scissors") && cpuChoice.equals("Scissors")))
            {
            winner = "Tie";
            }      
      else
            {
            JOptionPane.showMessageDialog(null, "Error choosing winer");
            }      
    
     return winner;
      
      //tie, user, computer
      
      
   }//end pickWinner
   
 }//end class

Link to comment
Share on other sites

Link to post
Share on other sites

 

Since I am to lazy to put something interesting here, I will put everything, but slightly abbreviated. Here is everything:

 

42

 

also, some questions to make you wonder about life:

 

What is I and who is me? Who is you? Which armrest in the movie theatre is yours?

 

also,

 

Welcome to the internet, I will be your guide. Or something.

 

 

My build:

CPU: Intel Core i5-7400 3.0GHz Quad-Core Processor,

 Motherboard: ASRock B250M Pro4 Micro ATX LGA1151 Motherboard, 

Memory: Corsair 8GB (1 x 8GB) DDR4-2133 Memory,

Storage: Seagate Barracuda 1TB 3.5" 7200RPM Internal Hard Drive, 

Video Card: MSI Radeon RX 480 4GB ARMOR OC Video Card, 

Case: Corsair 100R ATX Mid Tower Case , 

Power Supply: Corsair CXM 450W 80+ Bronze Certified Semi-Modular ATX Power Supply, 

Operating System: Microsoft Windows 10 Home Full, 

Wireless Network Adapter: TP-Link TL-WN725N USB 2.0 802.11b/g/n Wi-Fi Adapter, Case Fan: Corsair Air Series White 2 pack 52.2 CFM  120mm Fan

 

ou do not ask why, you ask why not -me

 

Remeber kinds, the only differ between screwing around and scince is writing it down. -Adam Savage.

 

Only two things are infinite: the universe and human stupidity, and I'm not even sure of the former. - Albert Einstein.

Link to comment
Share on other sites

Link to post
Share on other sites

the error is that the pickWinner method expects 4 parameters, but u only give him 2. You need to pass the string winner and player name to the parameters, like this 


 

winner = rps.pickWinner(userChoice, cpuChoice, winner, userName);

 

sry for my english :(

Link to comment
Share on other sites

Link to post
Share on other sites

You're probably confusing variable declaration with arguments. And, you don't need winner variable, you can return the winner (or tie) right away, without putting it to variable first. So if you would want to keep winner variable then you would need to do something like this:

public String pickWinner(String userChoice, String cpuChoice){
  String winner;
    
  //... rest of code

Then you do what you already have, check choices and put string into winner variable.

 

But as I said you don't need a variable.

public String pickWinner(String userChoice, String cpuChoice) //Notice there is no winner and player name variables.
{      
  if ((cpuChoice.equals("Rock") && userChoice.equalsIgnoreCase("paper")) ||
      (cpuChoice.equals("Paper") && userChoice.equalsIgnoreCase("Scissors")) ||
      (cpuChoice.equals("Scissors") && userChoice.equalsIgnoreCase("rock")))
  {  
    //winner = "User";
    return "User";
  }
  
  //... and so one for Computer and Tie
  
  //return winner; <- this won't be needed too.

BTW. If there is no winner, it is a tie, I guess there is no need for additional else if to check that. I would return "Tie" on else, so there would be no route that is not return

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, DemonBF said:

the error is that the pickWinner method expects 4 parameters, but u only give him 2. You need to pass the string winner and player name to the parameters, like this 


winner = rps.pickWinner(userChoice, cpuChoice, winner, userName);

 

 

sry for my english :(

Your english is fine Brother! That did the trick! 
PS: how did you get that text in that kind of font style?

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, Beeeyeee said:

Your english is fine Brother! That did the trick! 
PS: how did you get that text in that kind of font style?

I copy pasted ure code, probably it copied the font style too.

PS: I recomend you to look at the Mr_Koka answer, it will help you to avoid future problems!

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, DemonBF said:

I copy pasted ure code, probably it copied the font style too.

Yeah, be careful with that, use ctrl shift v to paste text without formatting, your code was invisible for me (on a dark forum skin) until selected.

(If Beeeyeee wouldn't mention that I would never find out there was a line of code in your post : D)

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, DemonBF said:

the error is that the pickWinner method expects 4 parameters, but u only give him 2. You need to pass the string winner and player name to the parameters, like this 


winner = rps.pickWinner(userChoice, cpuChoice, winner, userName);

 

 

sry for my english :(

Your english is fine Brother! That did the trick! 
PS: how did you get that text in that kind of font style?

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Mr_KoKa said:

Yeah, be careful with that, use ctrl shift v to paste text without formatting, your code was invisible for me (on a dark forum skin) until selected.

(If Beeeyeee wouldn't mention that I would never find out there was a line of code in your post : D)

Ok, thx for the info, will take more care next time!

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×