Brandon
Brandon

Reputation: 27

Bad operand type

public static void listRentalDetailsOverPeriod(ArrayList customerList)
{
Scanner input = new Scanner(System.in);
Customer c = null;
Vehicle v = null;

System.out.println("Enter Ic Number:");
String icNo = input.nextLine();

for(int i=0;i<customerList.size();i++)
{//start of for loop
Customer cust = (Customer) customerList.get(i);
if(cust.getIcNo().equalsIgnoreCase(icNo))
{
  c = cust;
  break;
}
}//end of for loop

ArrayList rentalList = c.getRentalList();

System.out.print("Enter Start Date(DD MM YYYY): ");
int dd = input.nextInt();
int mm = input.nextInt();
int yyyy = input.nextInt();

Calendar dob = new GregorianCalendar(yyyy,mm-1,dd);//creates calendar object

System.out.print("Enter End Date(DD MM YYYY): ");
int dd1 = input.nextInt();
int mm1 = input.nextInt();
int yyyy1 = input.nextInt();

Calendar dob1 = new GregorianCalendar(yyyy1,mm1-1,dd1);//creates calendar object

for(int i=0;i<rentalList.size();i++)
{//start of for loop
Rental r = (Rental) rentalList.get(i);//create rental object


if(r.getPickupDate() > dob && r.getReturnDate() < dob1)
{
System.out.println(r);
}


}//end of for loop
}

Error:

VehicleRental.java:660: error: 
bad operand types for binary operator '>'
if(r.getPickupDate() > dob && r.getReturnDate() < dob1)

I am having errors trying to compile this code, I am getting bad operand for binary operators. I am trying to display rental details of customer over a period.

Upvotes: 0

Views: 231

Answers (2)

Elliott Frisch
Elliott Frisch

Reputation: 201527

You can't compare Object instances with the < or > like that. You should be using Calendar.before(Object) and Calendar.after(Object) instead. Something like,

if(r.getPickupDate().after(dob) && r.getReturnDate().before(dob1))
{
    System.out.println(r);
}

Upvotes: 0

EDToaster
EDToaster

Reputation: 3180

Most likely, r.getReturnDate does not return a number value (integer, double, long, etc) I assume it returns a Date/Calendar object, and you cannot compare object with binary operators. That is why they are called BINARY operators ;)

Solution: Use: Calendar#before(obj) or #after(object)

if(r.getPickupDate().after(dob) && r.getReturnDate().before(dob1)){
    //do stuff
}

Upvotes: 1

Related Questions