0

I'm new to Java and learning - so excuse the potentially silly question that this might be!

It's a simple Paper Rock Scissors game...

Using BlueJ I keep getting this error;

"incompatible types"

When running this code;

import comp102.*; 

import java.util.Scanner;


public class RPS{

    String paper = "paper";
    String rock = "rock";
    String scissors = "scissors";    

public void playRound(){

        String paper = "paper";
        String rock = "rock";
        String scissors = "scissors";    

        System.out.print ('\f'); // clears screen
        Scanner currentUserSelection = new Scanner(System.in);

        String enterText = null;
        System.out.println("Make your Selection; Paper, Rock or Scissors: ");
        enterText = currentUserSelection.next();

        System.out.println("enterText = " + enterText);

        if(enterText = paper){
            System.out.println("the IF Stmt is working");
        }

    }

The error is referring to this line "if(enterText = paper){"

Many Thanks

  • You are welcome. If your problem is solved you can mark one of the below answer as accepted see this link [How does accepting an answer work?](http://meta.stackexchange.com/q/5234/203266) – Aniket Kulkarni Oct 29 '13 at 13:05

6 Answers6

1

You are trying to assign value in if which is not allowed

if(enterText = paper)  //here if expects expression which evaluates to boolean  

Change to,

if(enterText == paper)

From language specs jls-14.9

The if statement allows conditional execution of a statement or a conditional choice of two statements, executing one or the other but not both.

The Expression must have type boolean or Boolean, or a compile-time error occurs.

Instead of == operator use String#equals to compare the string.

if(enterText.equals(paper))  //this will compare the String values  

See also

Community
  • 1
  • 1
Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
0

Use

if(enterText == paper)

instead

Paul Draper
  • 78,542
  • 46
  • 206
  • 285
0

Change you if{..} condition as

if(enterText.equals(paper)){
  System.out.println("the IF Stmt is working");
}

Because you are assigning value within the if condition. So it is invalid.

Within if condition you have to check either it is true or false only.

Syntax for if(){..}

if(true or false) {
  //body
}
Naveen Kumar Alone
  • 7,536
  • 5
  • 36
  • 57
0
 if(enterText = paper){
            System.out.println("the IF Stmt is working");
 }

you should use == to check for equality. But since you are dealing with strings use equals() method

e.g.

 if(enterText.equals(paper)){
        System.out.println("the IF Stmt is working");
 }
0
if(enterText = paper){
  System.out.println("the IF Stmt is working");
}

Here you are using = That is assignment operator.

Where == is check for equality.

More over in java to check equality of Strings you should use equals()

Reason:Why doesn’t == work on String?

So your code becomes,

if(enterText.equals(paper)){
      System.out.println("the IF Stmt is working");
    }
Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0
    if(enterText = paper){
        System.out.println("the IF Stmt is working");
    }

should be

    if(enterText == paper){
        System.out.println("the IF Stmt is working");
    }
Joe
  • 1
  • 2