Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-10-2012, 08:05 PM   PM User | #1
abell12
New Coder

 
Join Date: Mar 2012
Posts: 31
Thanks: 1
Thanked 1 Time in 1 Post
abell12 is an unknown quantity at this point
If Statment Help

Can anyone tell me why the if statement never passes please. I have put the lines that are shown in the terminal window under the code. (the System.out.println() )
The bottom two System.out's are just a test to see if they are changing and they do but the if statement doesn't see they are the same.

Code:
int listPosition = 0;
        while(listPosition < basket.size())
        {
            Item item;
            item = basket.get(listPosition);
            if(itemName.toLowerCase() == item.getName().toLowerCase())
            {
                basket.remove(listPosition);
                System.out.println(itemName + " Removed");
            }
            else
            {
                System.out.println("Error");
                System.out.println();
            }
            System.out.println(itemName.toLowerCase());
            System.out.println(item.getName().toLowerCase());
            listPosition++;
        }
Terminal window shows this:
Code:
Error

apples
apples
So they are the same and the toLowerCase is working but for some reason the if statement doesn't see them as the same, why is this?

Thanks
Ash
abell12 is offline   Reply With Quote
Old 12-10-2012, 08:45 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Strings cannot be reliable compared using equality. Object equality compares the pointer, not the value, so if you do "A" == "A", then that is successful; however, if you do new String("A") == new String("A") that will not be successful as they are two distinct objects.

Use .equals or .equalsIgnoreCase to perform a comparison of string objects.
Fou-Lu is offline   Reply With Quote
Old 12-10-2012, 10:40 PM   PM User | #3
abell12
New Coder

 
Join Date: Mar 2012
Posts: 31
Thanks: 1
Thanked 1 Time in 1 Post
abell12 is an unknown quantity at this point
I changed it to.equalsIgnoreCase() but this throws and error at me.
Code:
if(itemName.equalsIgnoreCase() == item.getName().equalsIgnoreCase())
Can you modify my code so its right please.

Thanks
Ash
abell12 is offline   Reply With Quote
Old 12-10-2012, 11:07 PM   PM User | #4
abell12
New Coder

 
Join Date: Mar 2012
Posts: 31
Thanks: 1
Thanked 1 Time in 1 Post
abell12 is an unknown quantity at this point
Nevermind I figured out how to do it.
Thanks anyway.
abell12 is offline   Reply With Quote
Old 12-11-2012, 02:23 PM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
You betcha. Keep doing as many object comparisons as you can using the .equals method, and you're good to go. Use the comparison operators primarily for primitives or when you actually want to compare objects for identical check. Equals is defined from the core object, so you can easily override and define your own handling.
Two interfaces to look at as well are the Comparable interface which will grant you the int compareTo(T); method, and the Comparator interface which will grant you the int compare(T, T); method. These can both be used for equality check, as the result of the "same" object would be 0 in either of these, but are also useful for using Collections as the sorting algorithms will make use of the Comparator, or you can usually provide the sort with a Comparator.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:23 AM.


Advertisement
Log in to turn off these ads.