How should I deal with this bad operator error?

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 877 Views - Last Post: 30 November 2017 - 10:03 AM Rate Topic: -----

#1 mike455   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 12-November 17

How should I deal with this bad operator error?

Posted 23 November 2017 - 08:46 PM

Hello All,
I am working on creating a binary search tree array, but I am having trouble figuring out why I keep getting a bad operand types for a binary operator error inside of my insert and fetch operators. Do I need to use the compareTo() method instead of the > ? I get that it occurs because it telling that I can't use the operator for string types, right? Then how would I implement the insert and fetch methods for the binary search tree array without using that code? Please someone help? Here is my code so far.

public class Listing
{ private String name;  // key field
  private int ID;
   private double GPA;

  public Listing(String n, int id, double gpa)
  {  name = n;
      ID = id;
     GPA = gpa;
  }
   public String toString()
   {   return("Name is " + " "  + name +
                    "\nID is" + " " + ID +
                    "\nGPA is" + " "  + GPA + "\n");
   }
   public Listing deepCopy()
   {  Listing clone = new Listing(name, ID, GPA);
      return clone;
   }
   public int compareTo(String targetKey)
   {  return(name.compareTo(targetKey));
   }

   public String getKey()
   {  
       return name;
   }
 }// end of class Listing





public class BinaryTreeArray
{ 
   private Listing[] data; 
   private int size; 
   private int i = 0; 

  public BinaryTreeArray()
  { 
      size = 100; 
      data = new Listing[size]; 
  }
  public boolean insert(Listing newListing)
  { 
     while(i < size && data[i]!= null)
     { 
       if(data[i].getKey() > newListing.getKey())
         i = 2 * i + 1; 
        else 
         i = 2 * i + 2; 
     } 
     if(i >= size) 
       return false; 
     else 
     { 
         data[i] = newListing.deepCopy();
          return true;
      } 
  }
  public Listing fetch(String targetKey)
  {  
    while(i< size && data[i]!= null && data[i].getKey()!=targetKey)
    {
        if(data[i].getKey() > targetKey)
          i = 2 * i + 1;
       else
          i = 2 * i + 2;   
    }
    if(i >= size || data[i] == null)
      return null; 
    else 
      return data[i].deepCopy();
  }

} 
  


Is This A Good Question/Topic? 0
  • +

Replies To: How should I deal with this bad operator error?

#2 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12324
  • View blog
  • Posts: 45,424
  • Joined: 27-December 08

Re: How should I deal with this bad operator error?

Posted 23 November 2017 - 08:49 PM

In the future, it would be helpful if you posted the errors exactly as they appear, including line numbers. ;)

In your insert() method:
if(data[i].getKey() > newListing.getKey())



Now getKey() returns a String, and Java does not allow you to compare Strings using the >, >=, <, or <= operators. Hence, the error.
Was This Post Helpful? 0
  • +
  • -

#3 mike455   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 12-November 17

Re: How should I deal with this bad operator error?

Posted 23 November 2017 - 08:56 PM

But how do I fix this?
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12324
  • View blog
  • Posts: 45,424
  • Joined: 27-December 08

Re: How should I deal with this bad operator error?

Posted 23 November 2017 - 09:03 PM

The simple answer is not to compare Strings with operators that don't permit said comparison.

Are you trying to compare Listing objects based on the lexical ordering of the Strings, for inserting Listing objects into a BST?

Since your Listing object has a compareTo(String) method, I would suggest a couple small architectural changes. First, Java has a Comparable interface. I would suggest having your Listing class implement the Comparable<Listing> interface. Then rather than the compareTo(String) method, implement the compareTo(Listing) method. You can compare the Listing parameter object based on the keys:
public class Listing implements Comparable<Listing>{

    public into compareTo(Listing other){
        this.name.compareTo(other.name);
    }
}



Now you can compare two Listing objects using the compareTo() method:
int comparison = listing1.compareTo(listing2);


This post has been edited by macosxnerd101: 23 November 2017 - 09:05 PM

Was This Post Helpful? 0
  • +
  • -

#5 mike455   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 12-November 17

Re: How should I deal with this bad operator error?

Posted 23 November 2017 - 09:24 PM

If I do this way, I want be able to use the targetKey parameter inside of my fetch method? Is this the only other way to do it?
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12324
  • View blog
  • Posts: 45,424
  • Joined: 27-December 08

Re: How should I deal with this bad operator error?

Posted 23 November 2017 - 09:27 PM

I am assuming the "targetKey" is the name associated with the other Listing object. As you can see from the sample code in my last post, this is exactly what is accomplished.

Note that by using the Comparable interface here, your Listing class will be compatible with a number of built-in Java library tools such as built-in sorting methods. This is one advantage of implementing the Comparable interface.
Was This Post Helpful? 0
  • +
  • -

#7 mike455   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 12-November 17

Re: How should I deal with this bad operator error?

Posted 23 November 2017 - 09:58 PM

Okay, you are right, but what happens when I go to actually use that fetch in my main method? I won't be able to write use a string name instead it would have to be something like this. Let me know if you get what I am trying to say:

BinaryTreeArray data = new BinaryTreeArray(); 

 Listing l1  = new Listing("Carol", 4354,  3.2); 
    
data. insert(l1);
data.fetch(l1); 

// I am used to writing the name such as  string parameter such as "Carol" or "Bobby" now I have use a listing object to call the student's information?


 

Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12324
  • View blog
  • Posts: 45,424
  • Joined: 27-December 08

Re: How should I deal with this bad operator error?

Posted 23 November 2017 - 10:07 PM

The insert() method accepts a Listing object, not an int. So you cannot pass 11 as a parameter. Similarly, the fetch() method accepts a String, not an int.

You might have to rework your methods a little. I will leave this to you.
Was This Post Helpful? 0
  • +
  • -

#9 mike455   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 12-November 17

Re: How should I deal with this bad operator error?

Posted 23 November 2017 - 10:13 PM

l1 is a listing object. Did you think it was a number?

View Postmike455, on 23 November 2017 - 10:12 PM, said:

l1 is a listing object. Did you think it was a number?

Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12324
  • View blog
  • Posts: 45,424
  • Joined: 27-December 08

Re: How should I deal with this bad operator error?

Posted 23 November 2017 - 10:15 PM

I misread- my apologies.

My point in my last post still stands though- you may need to massage your methods a bit. I'll leave those tweaks to you.
Was This Post Helpful? 0
  • +
  • -

#11 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3562
  • View blog
  • Posts: 16,233
  • Joined: 20-September 08

Re: How should I deal with this bad operator error?

Posted 24 November 2017 - 03:05 AM

Quote

name = n;

No need to invent different names for parameters. See http://technojeeves....yle-conventions
Was This Post Helpful? 1
  • +
  • -

#12 mike455   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 12-November 17

Re: How should I deal with this bad operator error?

Posted 24 November 2017 - 11:22 AM

@g00se Care to add anything else that is actually related to the question in hand about this post? Do you have a different way that's is different from macosx's solution that could even been simpler without using the comparable interface? That's what I want to learn. Thanks!
Was This Post Helpful? 0
  • +
  • -

#13 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3562
  • View blog
  • Posts: 16,233
  • Joined: 20-September 08

Re: How should I deal with this bad operator error?

Posted 24 November 2017 - 11:30 AM

That's what i'd do. Follow mac!
Was This Post Helpful? 1
  • +
  • -

#14 mike455   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 12-November 17

Re: How should I deal with this bad operator error?

Posted 24 November 2017 - 11:34 AM

@g00se What changes would I need to make to the insert and fetch methods now that I implemented the comparable interface inside of the Listing class?

Specifically both of these lines in the insert and fetch methods:

 if(data[i].getKey() > newListing.getKey())

....

if( data[i].getKey() > targetKey)
...

Should I use the compareTo method inside of these lines?


This post has been edited by mike455: 24 November 2017 - 11:36 AM

Was This Post Helpful? 0
  • +
  • -

#15 ndc85430   User is online

  • I think you'll find it's "Dr"
  • member icon

Reputation: 849
  • View blog
  • Posts: 3,407
  • Joined: 13-June 14

Re: How should I deal with this bad operator error?

Posted 24 November 2017 - 11:42 AM

Have you read about the Comparable interface? See, for example this.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2