2

I am writing a compareTo method for the Comparable Interface for a specific class. I need to take two different strings and compare them and see if one holds a greater value than the other. I would normally use .compareTo() but in this case it is giving me an error and not working. This is what I have:

public int compareTo(Object o) {
    if (name < ((AccountOwnerComparable) o).name)
        return -1;
    else if (name > ((AccountOwnerComparable) o).name)
        return 1;
    else
        return 0;

}

I know I can't use the < or > but it is just to show what the scenario is.

0

4 Answers 4

1

You might have missed a bracket or something while casting. Try the following piece of code.

public int compareTo(Object o) {
    if (name.compareTo(((AccountOwnerComparable) o).name) > 0) {
        System.out.println("Greater");
        return -1;
    } else if (name.compareTo(((AccountOwnerComparable) o).name) < 0) {
        System.out.println("Lesser");
        return 1;
    } else {
        System.out.println("Equal");
        return 0;
    }

}
Sign up to request clarification or add additional context in comments.

Comments

1

The Comparable interface is generic. Your class declaration should probably look something like this:

class AccountOwnerComparable implements Comparable<AccountOwnerComparable> {...

Notice the AccountOwnerComparable type parameter. With this, you would have

public int compareTo(AccountOwnerComparable other) {
    return name.compareTo(other.name);
}

Comments

1

You just have to do:

name.compareTo(String_to_be_compare);

compareTo

public int compareTo(Object o)

Compares this String to another Object. If the Object is a String, this function behaves like compareTo(String).

Note that:

Returns:

The value 0 if the argument is a string lexicographically equal to this string; a value less than 0 if the argument is a string lexicographically greater than this string; and a value greater than 0 if the argument is a string lexicographically less than this string. (source)

So to your case:

public int compareTo(Object o)
{
    AccountOwnerComparable obj = (AccountOwnerComparable) o;
    return obj.name.compareTo(this.name);
}

tw: implement the comparable interface specifying what you want to compare to (usually the same type), since you did not specify anything your interface compares to anything which is odd.

Comments

0

Use Integer.parseInt() method. In this way you could use > or < and it is more verbose. I assume you want to compare two String objects containing numbers. Although you did not write it in the questions, the title says so.

2 Comments

They are not Integers/. Both are Strings just containing letters. The compareTo() method returns an integer when it compares two different Strings. That is what I need
well, the title was misleading.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.