Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8897801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:26:32+00:00 2026-06-15T00:26:32+00:00

I got this code: System.out.println(Enter the brand and cash value); String brand = keyboard.nextLine();

  • 0

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;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T00:26:33+00:00Added an answer on June 15, 2026 at 12:26 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got this simple code: String ip = 1.2.3.4; String[] ipArray = ip.split(.); System.out.println(ipArray[1]);
So I have this little piece of code: System.out.println(Size: + mounts.keySet().size()); for (JHttpPath entry
I got the following code: int nnames; String names[]; System.out.print(How many names are you
Do check this code: int result = 0; result = result++; System.out.println(Result 1 =
I got this code ` // // prints out Hello World! // hello_world(); //First
So I've got this code that compiles a class from a string and then
Got this code for a viewscroller from the apple developers site. @synthesize scrollView1, scrollView2;
I got this code from our frontend guy for headings: <h2 class=headline><span>Foobar</span></h2> The span
I got this code in order to build an url for the link using
I got this code from the wordpress <head profile=http://gmpg.org/xfn/11> What does this means? what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.