1

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.

JSm
  • 11
  • 4
  • 3
    incorrect signature of `equals()`, incorrect `;` after if statement – jmj Apr 15 '15 at 20:44
  • possible duplicate of [What issues should be considered when overriding equals and hashCode in Java?](http://stackoverflow.com/questions/27581/what-issues-should-be-considered-when-overriding-equals-and-hashcode-in-java) – Raedwald Apr 15 '15 at 20:53
  • @JSm why would you use many instances of the Scanner class and not just one? – alainlompo Apr 15 '15 at 21:09
  • what do you mean of incorrect signature of the equals? @JigarJoshi . You mean the (Cellphone phone)? – JSm Apr 15 '15 at 22:53
  • @alainlompo , I thought that you could only have one scanner per variable ( so one scanner for when I ask for the brand. So on and so forth) – JSm Apr 15 '15 at 22:54
  • How would you go to change that? I thought the equals method was like this: equals (Object object2) – JSm Apr 15 '15 at 23:07

3 Answers3

3

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.

Community
  • 1
  • 1
Mick Mnemonic
  • 7,808
  • 2
  • 26
  • 30
  • And also to add to this, when you are comparing two floating doubles use the compareTo method. You are comparing two prices which are of double data type, in the equals2 method. – Iqbal S Apr 15 '15 at 20:58
  • @Iqbal, that's a good point, but as no arithmetic happens with the `double`s, comparisons using the `==` operator probably work in this case. – Mick Mnemonic Apr 15 '15 at 21:10
  • So if I may ask, the use of cellphone in this case was comparing the brand to the whole object? ( that's what I understood). From my understanding, I thought that using (Cellphone phonne) and then using brand.equals(phone), I was only "selecting" the brand from the phone ( implicitely) – JSm Apr 15 '15 at 22:51
  • @JSm, yes, you were comparing a `String` object to a `Cellphone` object. Comparisons between objects don't work that way in Java. I've edited my answer to clarify how you should properly implement `equals` (and `hashCode`). – Mick Mnemonic Apr 16 '15 at 09:47
0

Change your last for-statement: old:

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

new:

for(int i=0; i<=cellphoneArr.length; i++)
Rafał P
  • 252
  • 1
  • 8
-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.