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 7547941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:35:06+00:00 2026-05-30T09:35:06+00:00

What I do not understand is why I am getting an error compiling my

  • 0

What I do not understand is why I am getting an error compiling my code when a String is in fact an object, and the compiler is saying otherwise. I dont know why I keep getting this error message

  symbol:   method compareTo(Object)
  location: variable least of type Object
.\DataSet.java:17: error: cannot find symbol
   else if(maximum.compareTo(x) < 0)

here is the code. I’m trying to utilize the class comparable to allow two objects to use the compareTo method. In the tester, I’m just trying to use a basic string object to compare.

public class DataSetTester
{
public static void main(String[] args)
{
    DataSet ds = new DataSet();
    String man = "dog";
    String woman = "cat";
    ds.add(man);
    ds.add(woman);
    System.out.println("Maximum Word: " + ds.getMaximum());


 }
}

Class:

public class DataSet implements Comparable 
{
 private Object maximum;
 private Object least;
 private int count;
 private int answer;

 public void add(Object x)
 {

   if(count == 0){
     least = x;
     maximum = x;
   }
   else if(least.compareTo(x) > 0)
     least = x;
   else if(maximum.compareTo(x) < 0)
    maximum = x;
   count++;
 }

 public int compareTo(Object anObject)
 {
     return this.compareTo(anObject);
 }

 public Object getMaximum()
 {
  return maximum;
 }

 public Object getLeast()
 {
   return least;
 }
}

Comparable Interface:

public interface Comparable
{
    public int compareTo(Object anObject);
}
  • 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-05-30T09:35:08+00:00Added an answer on May 30, 2026 at 9:35 am

    The problem is that DataSet implements Comparable, but Object doesn’t.

    Instead of storing Objects, you want to store Comparables. However, if you do get this to compile, you will get into an infinite loop right here:

    public int compareTo(Object anObject) 
    {
        // Yeah, never stop loopin'!
        return this.compareTo(anObject);
    }
    

    It’s recommended that in newer code, you use the generic Comparable<T> interface. Your code would then look like this:

    public class DataSet implements Comparable<DataSet>  
    {  
     private String maximum;  
     private String least;  
     private int count;  
     private int answer;  
    
     public void add(String x)  
     {  
    
       if(count == 0){  
         least = x;  
         maximum = x;  
       }  
       else if(least.compareTo(x) > 0)  
         least = x;  
       else if(maximum.compareTo(x) < 0)  
        maximum = x;  
       count++;  
     }  
    
     public int compareTo(DataSet anObject)  
     {  
         // I don't really know how you want this to work.
         // Come up with your own criteria on what makes a DataSet greater or less than
         // another one.
         count - anObject.count
     }  
    
     // Good practice to include this if you're doing a compareTo.
     @Override
     public boolean equals(Object other)
     {
         return (other instanceof DataSet) && compareTo((DataSet)other) == 0;
     }
    
     public String getMaximum()  
     {  
      return maximum;  
     }  
    
     public String getLeast()  
     {  
       return least;  
     }  
    }  
    

    Edit – just saw that you’re comparing strings. In that case, you don’t really need DataSet to implement Comparable. However, if you do need it for something else, what I wrote still stands.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting an error I do not understand. I am using File:Find to
I do not understand why i am getting this error. Here is the related
I am not able to understand the error with the code below which simply
All time am getting below error message in groovy... Could not understand whats causing
I am getting the Error/Warning about a part of my code saying 'Messages Without
Getting an error of : Error 1 'string RadioGroupTester.Form1.chkReceipt_CheckedChanged(object, System.EventArgs)' has the wrong return
I am getting an error I do not understand. There was even a similar
I do not understand this error, do not generate error in JsonResult Test (),
today i fot one error. i am not able to understand why it is.
I am unable to understand this error.This error is not in the class that

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.