1

I got this code:

    System.out.println("Enter the brand and cash value");

    String brand = keyboard.nextLine();

    long cash = keyboard.nextDouble();
    String buffer = keyboard.nextLine();

but even though I enter the exact String value I am trying to compare to, it fails to recognize they are the same. Strangely when I enter this:

compare[0] = new Car ("BMW", 12.00);

instead of this:

compare[0] = new Car (brand, 12.00);

it works

I also use equals:

public boolean equals(Car other)
{
    if (other == null)
    {
        return false;
    }

    if(this.brand == other.brand && this.cash == other.cash)
    {
        return true;
    }
    else
    {
        return false;
    }
}
mvp
  • 111,019
  • 13
  • 122
  • 148
Gladstone Asder
  • 383
  • 1
  • 7
  • 11

3 Answers3

5

You are using == to test String equality, and "BMW" is a String literal, which is interned in a pool whereas brand isn't. In other words, if you have:

String s1 = "BMW";
String s2 = "BMW";
String s3 = getString(); //receives "BMW" from the scanner

s1 == s2 is true
s1 == s3 is false
s2 == s3 is false
s1.equals(s2) is true
s1.equals(s3) is true
s2.equals(s3) is true

Bottom line: you should use equals to compare strings.

You can read more about it in this post.

EDIT

In the code of your equals method you need to change

if(this.brand == other.brand && this.cash == other.cash)

to this:

if(this.brand.equals(other.brand) && this.cash == other.cash)

Also note there are a few other issues with your equals - in particular, it does not override equals: it should be public boolean equals(Object o)

EDIT 2

You could implement your equals method like this for example (it assumes that brand can't be null - if it is not the case you need to handle that specific case too)

@Override
public boolean equals(Object obj) {
    if (obj == null || getClass() != obj.getClass()) {
        return false;
    }

    final Car other = (Car) obj;
    return (this.cash == other.cash && this.brand.equals(other.brand));
}

Note that you should also override the hashcode method.

Community
  • 1
  • 1
assylias
  • 321,522
  • 82
  • 660
  • 783
  • i use equals() that's probably not the problem – Gladstone Asder Nov 24 '12 at 07:45
  • @GladstoneAsder I understand that you overrode `equals` in your Book class. Can you show that code? I would imagine that within the `equals` method, you compare the brands with `if (this.brand == other.brand) {}`. – assylias Nov 24 '12 at 07:46
  • when i change car to object it gives me the following errors: cannot find symbols and are you sure that's the error? it worked in my earlier program. – Gladstone Asder Nov 24 '12 at 08:00
1

You need to use

this.brand.equals(other.brand)

in your if clause instead of

this.brand == other.brand

The == is used to check the reference of String and its value..

In this case your values are same but not the reference.

So you need to use equals cause it is used to check values only.
and thats what you want to do I guess.

Pratik
  • 1,531
  • 3
  • 25
  • 57
0

Use java.lang.Object's equal method as I've shown below

public boolean equals(Car other)
{
    if (other == null)
    {
        return false;
    }

    if(this.brand.equals(other.brand) && this.cash.equals(other.cash))
    {
        return true;
    }
    else
    {
        return false;
    }
}
Parth Soni
  • 11,158
  • 4
  • 32
  • 54
  • @vels4j It depends if cash is a `double` or a `Double`. It seems to be a `double` though in which case the `equals` thingy won't compile indeed. – assylias Nov 24 '12 at 10:26