2

sorry if my question looks very stupid. I get error on .compareTo() Cannot invoke compareTo(double) on the primitive type double! how can i fix this ? Thank you!

Vehicle class:

public class Vehicle implements IOutput {
private double cost;}

public double getCost(){
        return cost;
    }

Array Class:

public static void sortByVehicleMakeModel(Vehicle[] vehicles) {

    boolean swapped = true;

    for(int y = 0; y < vehicles.length && swapped; y++) {
        swapped=false;
        for(int x = 0; x < vehicles.length - (y+1); x++) {
            if(vehicles[x].getCost().compareTo(vehicles[x + 1].getCost()) > 0){
                swap(vehicles, x, x + 1);
                swapped=true;
            }
        }
    }
}

my other codes works fine:

public static void sortByOwnerName(Vehicle[] vehicles) {
    boolean swapped = true;

    for(int y = 0; y < vehicles.length && swapped; y++) {
        swapped=false;
        for(int x = 0; x < vehicles.length - (y + 1); x++) {
            if(vehicles[x].getOwner().getName().compareTo(vehicles[x + 1].getOwner().getName())> 0) {   
                swap(vehicles, x, x + 1);
                swapped=true;
            }
        }
    }
}
SamR
  • 517
  • 3
  • 10
  • 24

6 Answers6

3

Change the return type of your getCost() method from double to Double and it will all work. Auto boxing will take care of the rest.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • This will NOT work because there's no comparison operator (==, >, <)! It's not C++ with an implicit test against 0. – Andrew Lazarus Nov 17 '12 at 06:45
  • 1
    @AndrewLazarus: Please care to read the answer carefully before going around just downvoting carelessly. – Bhesh Gurung Nov 17 '12 at 06:50
  • @BheshGurung Well, all of you missed the real error, so you get downvoted. Truth is, I don't do that so often, but this was egregious. If getCost() is returning a `double`, the correct solution isn't boxing both sides (extremely slow operation), but using a vanilla comparison statement. – Andrew Lazarus Nov 17 '12 at 06:53
  • I wouldn't just blindly call it an extremely slow operation (specially without knowing the real context). Also, I don't see anybody talking about performance here. I think it's pretty clear that the OP is trying to invoke that `compareTo` method. – Bhesh Gurung Nov 17 '12 at 06:57
  • @BheshGurung. Is he trying to invoke it because he forgot getCost() returns a double? Because he just copy/pasted from the other field? If it's a primitive, then compareTo is overkill. It might be that the compiler sees the boxing/unboxing is a waste and optimizes it out. If not, it's slow, given that the basic question is a simple compare-and-branch. – Andrew Lazarus Nov 17 '12 at 07:01
  • I changed this to : public Double getCost() in stead of public double getCost(){ return cost; } – SamR Nov 17 '12 at 07:15
  • @Niloo The difference is double is a primitive type, which have no methods, but Double is an object thats a very thin wrapper around a primitive double value which allows the primitive to be used as an object and provides some methods. And the "boxing" process is extremely fast... the other guy who said it was slow doesn't know what he's talking about. – Bohemian Nov 17 '12 at 07:26
1

if(vehicles[x].getCost().compareTo(vehicles[x + 1].getCost()))

You need >0 in there somewhere!

Andrew Lazarus
  • 18,205
  • 3
  • 35
  • 53
  • if(vehicles[x].getCost().compareTo(vehicles[x + 1].getCost()) > 0){ didn't work and same error!! – SamR Nov 17 '12 at 06:48
  • 1
    In that case, you have TWO errors. If getCost returns a `double` (small d), don't use compareTo. Just use plain old `if (a.getCost() > b.getCost())` If it returns a `Double`, compareTo should work and you have a typo somewhere. [If you want, you can use the `Double` stuff on `getCost()` which returns a `double`, but that is _very_ inefficient.] – Andrew Lazarus Nov 17 '12 at 06:50
  • 1
    wow Andrew, I learned a lot from you Thank you! :D ... & I had NO what so ever Idea that a d or D can make a big difference ;) – SamR Nov 17 '12 at 07:23
1

compareTo method is not available on premitive type. Use Wrapper Double as:

     if(Double.valueOf(vehicles[x].getCost())
          .compareTo(Double.valueOf(vehicles[x + 1].getCost()))>0){

Please Note: Double.valueOf(double) returns the Wrapper type Double with value as double.

Please Note: If your objective is to use compareTo then its fine otherwise, you may want to directly compare double values using comparison operators <, >, == as appropriate.

Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
0

You can only invoke methods on a reference type, double is a primitive type. As the error message suggests vehicles[x].getCost() returns a double.

One thing you can do is manually box your double into Double:

int costComp = Double.valueOf(vehicles[x].getCost()).compareTo(Double.valueOf(vehicles[x + 1].getCost());

if(costComp < 0) {
    //...
} else if(costComp == 0) {
    //...
} else {
    //...
}
Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
0

This code of yours works fine

if(vehicles[x].getOwner().getName().compareTo(vehicles[x+1].getOwner().getName())> 0)

because vehicles[x+1].getOwner().getName() must be returning an object of String and compareTo method accepts an object as an argument.

This code doesn't work

if(vehicles[x].getCost().compareTo(vehicles[x + 1].getCost()))

because vehicles[x + 1].getCost() must not be returning an object( in your case it must be returning a primitive double ) so there is mismatch in the type and the compiler complains that there is no such compareTo method that accepts double (a primitive)

Abubakkar
  • 15,488
  • 8
  • 55
  • 83
0

I changed this to :

public Double getCost() 

instead of

public double getCost(){ 
return cost; 
}
SamR
  • 517
  • 3
  • 10
  • 24
  • 1
    Because the little-d double **is not an object,** so you can't call methods on it. If you could, you could write `4.3.compareTo(-11.8);` So you did have TWO errors, one about using boxes, and one about comparing to zero. Personally, I would revert to returning a double and _not_ using compareTo; you incur needless overhead this way. – Andrew Lazarus Nov 17 '12 at 07:20
  • @Andrew thank you, but it was required by my professor. Sorry for trouble ,but I learned a lot :) – SamR Nov 17 '12 at 07:25