• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

comparing Stings help

 
Eric Johns
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(Class project)I have a simple console program that I wrote that takes user input and search a data file for a certian criteria the , but I want to add a second condition that would only show cars that meet the fitweigh and the user's car type preferences. so I added this like E.x( If ((fitWeight >=80) && (carType==type)) now when I did that I don't get any output at all no matter what, I don't even get anything even if I just run carType==type so is their something you have to due differently when comparing strings?
if (fitWeight >=80)

{
System.out.println();
System.out.println("Make: "+ make);
System.out.println("Model: "+ model);
System.out.println("Type: "+ type);
System.out.println("Retail Price: "+ retailPrice);
System.out.println("Safety Rating: "+ safetyRating);
System.out.println("Miles Per Gallon: "+ milesPerGallon);
System.out.println("Fit: "+ fitWeight);
System.out.println();

}
 
Kristol Crawley
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't think you could user "=" to compare String vars. Eric, use equals to do a string comparison. If case doesn't matter use equalsIgnoreCase.
Good luck!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To expand:
When comparing objects, the == operator is a test for identity not equality. It will check if the two objects are the exact same object instance, not whether their values are the same. For that, you would use the String.equals() or String.equalsIgnoreCase() methods as previously pointed out.
This makes complete sense if you understand that object variables don't contain the objects, but rather contain a reference to an object. So it makes complete sense that == would compare the two object references and only evaluate to true if the references are equal; which will only happen when the references are to the same object.
hth,
bear
 
Eric Johns
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
To expand:
When comparing objects, the == operator is a test for identity not equality. It will check if the two objects are the exact same object instance, not whether their values are the same. For that, you would use the String.equals() or String.equalsIgnoreCase() methods as previously pointed out.
This makes complete sense if you understand that object variables don't contain the objects, but rather contain a reference to an object. So it makes complete sense that == would compare the two object references and only evaluate to true if the references are equal; which will only happen when the references are to the same object.
hth,
bear

ok that makes sense ,but how do you use the String.equals()?
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adjusting the example from your original post to make use of String.equals()...
carType.equals(type)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic