Search code examples
javasyntaxbluej

Incompatible types: java.lang.String cannot be converted to boolean


I am trying to call a method within an if statement but I keep getting the following error.

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

When you run the getName method it should check the barcode the user enters and if it matches, it will return a String.

This is the class and method I am doing the method call.

public class ItemTable

   public String getName (Item x)
   {
    String name = null;

    if (x.getBarcode ("00001")) 
        name = "Bread";

    return name;
   }

This is the method/class I am calling from.

public class Item

private String barcode;

public Item (String pBarcode)
{
    barcode = pBarcode;
}

public String getBarcode (String barcode)
{
    return barcode;
}

Solution

  • I've never seen a getter method receiving parameters. The getBarcode method should return the actual barcode of your Item object, right? The one you send to the constructor method. If your answer to above question is yes, then the getBarcode method needs no arguments and the if should be modified, example:

    public String getBarcode()
    {
    return barcode;
    }
    

    And

    if(x.getBarcode().equals("00001"))
        name = "Bread";