compareTo() method for a string giving out and bad operand type error

59 views Asked by At

i'm doing a compareTo() method for a string the way i was taught but it's giving me an error, it seems that the way i formulated it similar to how you would usually do for a number doesn't go well with a string. Class Car(implements the interface: Comparable<> ) - Attributes: o Unique number plate (final) : String o Brand: String o Rental rate: double o Extra three (from your choice) - Methods: o Constructors: zero-arg and multi-arg o getters o setters (if needed) o toString() o equals(): compare plate numbers o compareTo(): compare plate numbers

import java.io.*;
import java.util.*;

public class RentalCars implements Comparable<RentalCars> {
  private final String UNP;
  private String brandName;
  private double rental_Rates;
  private int wheel_Drive;
  private String color;
  private int milage;
  
  
  
public RentalCars(){
      this(null,null, 0.0, 0, null, 0);
  }
public RentalCars(String UNP, String brandName, double rental_Rates, int wheel_Drive,
   String color, int milage){
    
      this.UNP = UNP;
      this.brandName=brandName;
      this.rental_Rates=rental_Rates;
      this.wheel_Drive=wheel_Drive;
      this.color=color;
      this.milage=milage;
  }
public String getUNP() {
    return UNP;
    }
public String getbrandName() {
     return brandName;
    }
public void setbrandname(String brandName) {
    this.brandName=brandName;
    }
public double getrental_Rates() {
    return rental_Rates;
    }
public void setrental_Rates(double rental_Rates) {
    this.rental_Rates=rental_Rates; 
    } 
public int getwheel_Drive() {
    return wheel_Drive;
    }
public void setwheel_Drive(int wheel_Drive) {
    this.wheel_Drive=wheel_Drive; 
    }
public String getcolor() {
    return color;
    }
public void setcolor(String color) {
    this.color=color; 
    }
public int getmilage() {
    return milage;
    }
public void setmilage(int milage) {
    this.milage=milage; 
    }

@Override
public String toString()
{
    return "the Number Plate of the car is "+UNP+"the Car brand is "+brandName+
            "the rent rate of this car is "+rental_Rates+"the wheel drive is "+
            wheel_Drive+"the color of the car is "+color+"the milage is "+milage;
 }
@Override
public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        if (!super.equals(obj)) {
            return false;
        }
        
        RentalCars other = (RentalCars) obj;
        return Objects.equals(UNP, other.UNP);
    }
public int compareTo(RentalCars rc){
   if(UNP>rc.UNP)return 1;
   if(UNP<rc.UNP)return -1;
   return 0;
  }
}
2

There are 2 answers

0
Elliott Frisch On

UNP is a String, not a number. You can't use > and < on String(s). But you can delegate the compareTo call to the String(s). Like,

public int compareTo(RentalCars rc){
   return UNP.compareTo(rc.UNP);
}
0
Hadi Soufan On

you can't use comparison operators on set of characters. you can use .compareTo() that returns 0 if both strings are equal , -1 if string 1 is less than string 2 , and 1 if string 2 is greater than string 1. so it will compare characters in ASCII code together (a is less than b since a is 97 in ASCII table but b is 98).