Search code examples
javacompile-timebluejgreenfoot

BlueJ error: incompatible types


Complete noob at Java here. School just started and I'm taking APCS. Our teacher showed us this cool class called Scanner, but he hasn't taught us it yet. I thought it was pretty cool, so I decided to research it more. After I did a little research, I found out that it basically allows you to use information imputed into the System into your code. I tried to make my own little program using this code. The idea is very simple.

  1. Ask the user to input his first name.

  2. After receiving the users first name, ask him to verify that it is his name (no spelling errors, wrong name, etc.).

  3. Use if/else statement, if yes: print a thank you. if no: print a random string making fun of him.

At this point I intended to use and if/else statement. This is something that we as a class have yet to cover. After typing out my if else statement, I got a compile-time error. Here's what BlueJ gave me:

incompatible type

This is the code:

public class useInfo
{
public static void main(String[] args)
{
    String firstName;
    String verifyFirstName;
    Scanner keyboardInput = new Scanner(System.in);
    Scanner verification = new Scanner(System.in);
    System.out.print("Please Enter your first name here --->");
    firstName = keyboardInput.nextLine();
    System.out.println("Thank you! Your first name is " + firstName + " , correct?");
    keyboardInput.close();
    verifyFirstName = verification.nextLine();
    if (verifyFirstName = "Yes") //this is the section I get the error on. RIGHT HERE.
    {
        System.out.println("Great! Thanks for you time.");
    }
    else
    {
        System.out.println("You need to get your stuff together. How can you not even type your own name correctly?");
    }
}
}

Solution

  • This is an assignment operator.

    verifyFirstName = "Yes"
    

    It's not checking to see if verifyFirstName is equal to "Yes", it's setting it equal to "Yes".

    You get the error "incompatible type" because the if statement expects something that evaluates to a boolean true or false. The assignment you put in parentheses evaluates to a String. You need to compare your String values with the equals method.

    if (verifyFirstName.equals("Yes")) {
        ...
    }