File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes comparing Stings help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "comparing Stings help" Watch "comparing Stings help" New topic
Author

comparing Stings help

Eric Johns
Greenhorn

Joined: Sep 13, 2003
Posts: 20
(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

Joined: Sep 17, 2003
Posts: 7
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

Joined: Jan 10, 2002
Posts: 61589
    
  67

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


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Eric Johns
Greenhorn

Joined: Sep 13, 2003
Posts: 20
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

Joined: Dec 10, 2001
Posts: 7023
Adjusting the example from your original post to make use of String.equals()...
carType.equals(type)


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: comparing Stings help