6 Replies Latest reply: Oct 9, 2012 3:43 AM by Kayaman RSS

    Incompatible types required boolean found string

    user7188033
      I am trying to set a value for a variable called Location when ever the value for the location is "AFRICA"
      I need to reset it to be "55555".

      What I am getting is that 'Incompatible types required boolean found string' under the if statemant. Can anyone shade some light on this?

      String Location = filename.substring(12,17);
      String AP = "AFRICA";
      System.out.println(Location);
      if (Location = AP)
      {
      System.out.println(Location);
      Location = "55555";
      }

      thanks
        • 1. Re: Incompatible types required boolean found string
          rp0428
          if (Location = AP) 
          { 
          System.out.println(Location);
          Location = "55555";
          }
          The IF condition needs to be a BOOLEAN. The '=' is used to do an assignment. The boolean would be '=='.

          But to compare strings why aren't you using
              if (Location.equalsIgnoreCase(AP) {
          • 2. Re: Incompatible types required boolean found string
            user7188033
            Thanks!

            I used (Location.equalsIgnoreCase(AP) and that took care of it.

            I am getting rusty!
            • 3. Re: Incompatible types required boolean found string
              966813
              if condition should be like this ....


              if ( condition which returns a boolean value )


              so, generally we will compare 2 or more values instead of the condition which finally results in a boolean value.

              But you are trying to assigning a value ( Location = AP ) instead of checking whether they are equal or not.

              As String is an immutable object, use equals() or equalIgnore() instead of ' == ' operator.


              All the very best,

              AnanthaNag KUNDANALA.
              xxxxxxxxxxxxxxxxxxxxxxxxxxxxx

              Edited by: sabre150 on Oct 9, 2012 6:45 AM

              Moderator action : email address removed.

              Edited by: sabre150 on Oct 9, 2012 6:47 AM
              • 4. Re: Incompatible types required boolean found string
                sabre150
                963810 wrote:
                As String is an immutable object, use equals() or equalIgnore() instead of ' == ' operator.
                Once more you are spouting rubbish. The fact that String is immutable is irrelevant here!
                • 5. Re: Incompatible types required boolean found string
                  TPD-Opitz
                  rp0428 wrote:
                  The IF condition needs to be a BOOLEAN. The '=' is used to do an assignment. The boolean would be '=='.
                  But to compare strings why aren't you using
                  if (Location.equalsIgnoreCase(AP) {
                  To stress this a little more:
                  <tt>==</tt> applied between References will compare the Adresses they point to. This means this will only be true if both References point to the identical Object.
                  @Test
                  testStringEquality(){
                     String s1 = "my Test"
                     String s2 = "Test"
                    Assert.assertTrue("This strings are equal",s1.equals("my "+s2));
                    Assert.assertFalse("This strings are not identical",s1 == ("my "+s2));
                  }
                  So use <tt>==</tt> only for primitive number types (those starting with a lower case letter).

                  bye
                  TPD
                  • 6. Re: Incompatible types required boolean found string
                    Kayaman
                    TPD Opitz-Consulting com wrote:
                    This means this will only be true if both References point to the identical Object.
                    Not identical. The same Object. Identical objects will be identified by the equals() and hashCode() methods.