Why am I getting this error? “bad operand types for binary operator '>'”

前端 未结 3 664
感动是毒
感动是毒 2021-01-16 14:32

I would like to know what it causing the error of \"bad operand types for binary operator \'>\'\" down below I have the codes for both my Hand and C

相关标签:
3条回答
  • 2021-01-16 15:02

    getCardFace() returns a String. < and > operators exist only for numeric types.

    You can use c1.getCardFace().compareTo(c.getCardFace()) < 0 or c1.getCardFace().compareTo(c.getCardFace()) > 0 instead, to compare the Strings according to their natural ordering.

    if ( c1.getCardFace() > c.getCardFace() ||
                    (c1.getCardFace().equals(c.getCardFace()) &&     c1.getFaceValue() < c.getFaceValue()) ) {
    

    would become

    if ( c1.getCardFace().compareTo(c.getCardFace()) > 0 ||
                    (c1.getCardFace().equals(c.getCardFace()) &&     c1.getFaceValue() < c.getFaceValue()) ) {
    

    and

    if ( c1.getFaceValue() < c.getFaceValue() ||
                    (c1.getFaceValue() == c.getFaceValue() && c1.getCardFace() > c.getCardFace()) ) {
    

    would become

    if ( c1.getFaceValue() < c.getFaceValue() ||
                    (c1.getFaceValue() == c.getFaceValue() && c1.getCardFace().compareTo(c.getCardFace()) > 0) ) {
    
    0 讨论(0)
  • 2021-01-16 15:13

    getCardFace() is returning String value but you can't use < , > , <= or >= for comparing String.

    0 讨论(0)
  • 2021-01-16 15:18

    Don't use these operators <,> and == to compare two Strings, instead use compareTo method.

    From Javadoc:

    public int compareTo(String anotherString)

    Compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string. The result is a negative integer if this String object lexicographically precedes the argument string. The result is a positive integer if this String object lexicographically follows the argument string. The result is zero if the strings are equal; compareTo returns 0 exactly when the equals(Object) method would return true.

    An example of comparing two Strings

    String s1="example1", s2="example2";
    if ( s1.compareTo(s2) > 0 )
         System.out.println("First string is greater than second.");
    else if ( s1.compareTo(s2) < 0 )
          System.out.println("First string is smaller than second.");
    else   
          System.out.println("Both strings are equal.");
    

    Note: The compareTo method is case sensitive i.e "java" and "Java" are two different strings if you use compareTo method. String "java" is greater than "Java" as ASCII value of 'j' is greater than 'J'. If you wish to compare strings but ignoring the case then use compareToIgnoreCase method.

    public int compareToIgnoreCase(String str)

    Compares two strings lexicographically, ignoring case differences. This method returns an integer whose sign is that of calling compareTo with normalized versions of the strings where case differences have been eliminated by calling Character.toLowerCase(Character.toUpperCase(character)) on each character.

    0 讨论(0)
提交回复
热议问题