-1

I want to set a boolean value to either true or false.I kept data base type as java.lang.boolean. and I want to write a code If the checkbox is selected It should be saved and I should be able to see in the frontend part. Can anyone suggest me with the right code. Below is my code

public Boolean setSelected(Boolean abool)
{
    if (abool=="Y")
        abool = true;
    else
        abool = false;

}

In this code I am missing something and getting error Incompatible operand types Boolean and String.

Rockstar
  • 2,228
  • 3
  • 20
  • 39
icesu
  • 47
  • 8

1 Answers1

1

From the Docs:

boolean: The boolean data type has only two possible values: true and false. Use this data type for simple flags that track true/false conditions. This data type represents one bit of information, but its "size" isn't something that's precisely defined.

In your code you're trying to compare a string value with a boolean which caused exception. You can use something like below to compare a string value and return a boolean.

 public boolean setSelected(String abool){
        boolean status = false;
        if ("Y".equals(abool))
            status = true;
        return status;
    }
Rockstar
  • 2,228
  • 3
  • 20
  • 39
  • can I use "Boolean" instead of "boolean" ?? @Rockstar – icesu Jan 11 '16 at 07:06
  • 1
    Yes you can use Boolean/boolean instead. First one is Object and second one is primitive type. On first one, you will get more methods which will be useful. Second one is cheap considering memory expense. – Rockstar Jan 11 '16 at 07:13
  • Hi I have one overridden method in JcheckBox class.So Iam supposed to use that method only. the method is "public void setData(boolean aBool){}".What logic should I write in this method like u wrote above@Rockstar – icesu Jan 11 '16 at 07:19
  • can you please add some more context to your requirement. What `setData(boolean aBool)` mean for? what it the expected behavior? – Rockstar Jan 11 '16 at 07:30
  • public void setDataChanged(boolean aBool) { } what logic shouls I write in this method to check whether the checkbox is checked or not.Can any one please answer this and help me – icesu Jan 11 '16 at 10:12
  • if aBool=="y" I have to return true else false .Iam supposed to check whether the check box i selected or not.@Rockstar – icesu Jan 11 '16 at 10:16
  • 1
    @icesu you can't return a value from `public void setDataChanged(boolean aBool) ` as the return type is declared as `void`. You're accepting an arguemnt of type `boolean` so either the comparison should be like : aBool==true or aBool==false. Else change the argument type to String if you want to validate it against any other string value. – Rockstar Jan 11 '16 at 10:31