Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8941275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:15:45+00:00 2026-06-15T11:15:45+00:00

I am writing a compareTo method for the Comparable Interface for a specific class.

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T11:15:46+00:00Added an answer on June 15, 2026 at 11:15 am

    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;
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing an app where I need to compare two :datetime fields and get
I'm writing specs for different test cases for Jasmine and QUnit to compare them
Im writing a php script to compare the similarity of 2 strings. This works
As part of the program that I am writing I need it to: Compare
I'm writing a JUnit test method for a Clock class to ensure that the
I'm having some trouble writing a hashCode() method for a class I created. This
I'm writing a class that have a method of removing an object from other
Possible Duplicate: How to compare two arraylist? I have two String ArrayLists of different
Am writing a SOAP web service method to compare a template with a feature
Currently I am writing a compareTo method for quadratic functions in the form: ax^2

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.