JSm
JSm

Reputation: 11

Logic error using equals method

So my program is suppose to take the input from a user about the brand, serial number, and price of a cellphone. Then, if the user wants, the program will compare the entered values ( namely brand and price in my case) and compare it to the objects from the array to see if any matches occur. However, eclipse just terminates when it comes to my if-else statement ( the last one) whether or not the user inputs "true" or false"

Here's ( part of) the code:

import java.util.Scanner;

class Cellphone {

private String brand;
private long serialNumber;
private double Price;

public Cellphone (String br, long sN, double Pr)
{
    brand= br;
    serialNumber = sN;
    Price = Pr;
}
public boolean equals(Cellphone phone)
{
    if (Price == phone.Price  && brand.equals(phone))
        return true;
    else
        return false;}
public boolean equals2(Cellphone phone)
{ if (Price == phone.Price)
    return true;
else
    return false; 
}
public boolean equals3(Cellphone phone)
{ if (brand.equals(phone));
    return true;
}

}
 public class CellPhoneSearch {

public static void main(String[] args) {
    // TODO Auto-generated method stub
Cellphone[] cellphoneArr = new Cellphone[10];
        cellphoneArr [0] = new Cellphone ("Samsung", 123456789, 500.5);
        cellphoneArr [1] = new Cellphone ("HTC", 123459876, 850.3);
        cellphoneArr [2] = new Cellphone ("Sony", 543216789, 1230.4);
        cellphoneArr [3] = new Cellphone ("Acer", 987654321, 600);
        cellphoneArr [4] = new Cellphone ("Razr", 543298761, 700);
        cellphoneArr [5] = new Cellphone (cellphoneArr [1]);
        cellphoneArr [6] = new Cellphone (cellphoneArr [2]);
        cellphoneArr [7] = new Cellphone (cellphoneArr [3]);
        cellphoneArr [8] = new Cellphone (cellphoneArr [4]);
        cellphoneArr [9] = new Cellphone (cellphoneArr [5]);


     System.out.println("Please enter the brand");
     Scanner userPreference = new Scanner(System.in);
     String userBrand = userPreference.nextLine();

     System.out.println("Please enter the serial number");
     Scanner userSN = new Scanner(System.in);
     long userSerialNumber = userSN.nextLong();

     System.out.println("Please enter the Price");
     Scanner userPr = new Scanner(System.in);
     Double userPrice = userPr.nextDouble();


Cellphone cell_1 = new Cellphone( userBrand, userSerialNumber, userPrice  );

Scanner input = new Scanner(System.in);
System.out.println("Please enter true or false if you'd like to conduct a search to see whether or not two or more cellphones are similar");
boolean enteredValue = input.nextBoolean();

if (enteredValue == true)
   { 
    for(int i=0; i<=cellphoneArr.length-1; i++)
      { System.out.println("hello");
        if (cell_1.equals(cellphoneArr[i]))
       System.out.println(cellphoneArr[i].toString());
      }


      }
else
   for(int i=0; i>=cellphoneArr.length; i++)
      {if (cell_1.equals2(cellphoneArr[i]))
          System.out.println(cellphoneArr[i]);

       }

at my last if - else statement, it seems as if the statement

 cell_1.equals(cellphoneArr[i]);

can't be evaluated. Eclipse compiles the program just fine but I don't see the result that I'm trying to get.

Upvotes: 1

Views: 131

Answers (3)

Mick Mnemonic
Mick Mnemonic

Reputation: 7956

You're comparing apples to oranges (or a String to a Cellphone) here:

brand.equals(phone);

which is why Cellphone.equals() always returns false. You should compare the brand Strings of the phones instead:

brand.equals(phone.brand);

Also, by convention, you should declare the equals method like this:

@Overrride
public boolean equals(Object obj)

The method argument should have type Object (not Cellphone) in order to properly override Object.equals(), which is what you should be doing.

Finally, if you override equals, you should also override Object.hashCode(), so that the hash code is calculated from the same fields that are used for equality comparison. This is because equal objects must have equal hash codes. This answer tells you all you need to know about the implementation details of equals and hashCode.

Upvotes: 3

Alex Snow
Alex Snow

Reputation: 1

I'm not sure if the IF statement in the equals method is a typo, but there should be a return statement instead. If it is indeed a typo then another reason to this could be the fact that the Price field, just like the other ones, is private and you might be calling the field outside of the class but I'm not sure if that would cause the problem. Hope it helps.

Upvotes: -1

Rafał P
Rafał P

Reputation: 252

Change your last for-statement: old:

for(int i=0; i>=cellphoneArr.length; i++)

new:

for(int i=0; i<=cellphoneArr.length; i++)

Upvotes: 0

Related Questions