0

I have an issue where I am trying to compare an object's name (String) to another String, and I've tried using .equals() as well as == but neither seem to work in my case.

The code I have right now is:

public boolean checkingObjectName(String checkName)
{
    for (int i=0; i<count; i++) //where 'count' has a value of 3
    {
        if (product[i].getName().equals(checkName)) //where product[i] has been initialised
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

The program always returns false, even if the name that has been set to product[i] is the same as the parameter given. I've tried looking at other questions relating to .equals() and == and other String references but I have not had any luck in finding something that relates to my problem.

I have seen people use compareTo(), but I'm not sure if that is necessary in my code, and I'm not completely sure on how to use it.


EDIT: As said by Houssni in the comments, I have just realised that the return statement ends the loop and method. Is it possible to check each product's name without having the method terminated after the first check, and have it return a boolean value?

EDIT 2: I have modified the code to how the answers that have been provided say, which is:

public boolean checkingObjectName(String checkName)
{
    for (int i=0; i<count; i++) //where 'count' has a value of 3
    {
        if (product[i].getName().equals(checkName)) //where product[i] has been initialised
        {
            return true;
        }
    }
    return false;
}

I still get the same issue with the product[i].getName() not being equal to checkName and the method returning false, even though they should equal. Any possibilities on why this is happening, because I've looked through my code and I'm not sure.

Edit 3: The only other code that relates to the block of code above is the input of the parameter from another class, which is:

String checkName = JOptionPane.showInputDialog("Enter the name: ");
while (storingProducts.checkingObjectName(checkName) == false) //assume for storingProducts
{
    JOptionPane.showMessageDialog(null, "No products were found. Please re-input name.");
    checkName = JOptionPane.showInputDialog("Enter the name: ");
    storingProducts.checkingObjectName(checkName);
}
if (storingProducts.checkingObjectName(checkName) == true)
//extra code

So, that's all the code relating to my issue. I'm still not sure why the method returns false, though both values are receiving the same String value.

Edit 4: The product and count are coming from the class with the checkingObjectName method:

public class Store
{
    private Product[] product; //Product is another class that just sets and gets
    private int count=3;

    public Store()
    {
        product = new Product[count];
        for (int i=0; i<count; i++)
        {
            product[i] = new Product();
        }
    //extra code

SOLUTION

I fixed the issue: instead of using .equals(), I tried using .equalsIgnoreCase() and it worked.

3
  • post all your code where you test it and initialised the product array Commented Jun 8, 2014 at 22:55
  • Why the loop if it only loops once? The return statement terminates the loop and the method. Commented Jun 8, 2014 at 22:57
  • if !product[0].getName().equals(checkName) the loop stops and the method returns false Commented Jun 8, 2014 at 22:58

4 Answers 4

7

The method is returning as soon as it hits count 0 and it doesn't find a match. If you want to loop through your array until you find a matching name, your code should look like this:

public boolean checkingObjectName(String checkName)
{
    for (int i=0; i<count; i++) //where 'count' has a value of 3
    {
        if (product[i].getName().equals(checkName)) //where product[i] has been initialised
        {
            return true;
        }
    }
    return false;
}
Sign up to request clarification or add additional context in comments.

8 Comments

I've modified the code to what you have clarified for me, but the method is still returning false, even though the parameter is - for example - Smoothie and product[i].getName() also equals Smoothie.
i being what value?
i has the value of 0, but gets incremented each time that product[i].getName() is not equal to checkName. Eventually, if each product does not have the same value as checkName, then it exits the loop and the method returns false.
Yes, I can see as much. But which element are you expecting to match? And on what basis do you believe it contains this particular string?
Well, with the rest of my code, I'm expecting to match product[0].getName() with the parameter, and I know that the Strings are the same because, for example, the String "Smoothie" is being set as the name for product[0] and the parameter being given is being set to "Smoothie" as well.
|
1

There are two ways to compare strings:

  • The first is to compare via addresses of the string. Such as: string blue = string red.
  • The second way to compare strings is through values. This can be done by using the string.equals("StringValue").

Comments

0

Since you have a return statement that will be reached on every posible situation (if condition is true or false), the method will always exit on the first iteration.

What can you do? If you want to return true if the name exists in the array and false otherwise, you can do:

public boolean checkingObjectName(String checkName)
{
    for (int i=0; i<count; i++) //where 'count' has a value of 3
    {
        if (product[i].getName().equals(checkName))
            return true;
    }
    return false; // if after all elements has been checked 
                  // and none was equal to 'checkName', return 'false'
}

Comments

0

How about collecting the names into a List (or Set) and check if checkName matches?

public boolean checkObjectName(final String checkName) {    
    List<String> productNames = new ArrayList<String>();
    for (Product prod : product) {
        productNames.add(prod.getName());
    }
    return productNames.contains(checkName);
}

Note that this will only work if you're comparing case-sensitive strings. Alternatively,

public boolean checkObjectName(final String checkName) {    
    List<String> productNames = new ArrayList<String>();
    for (Product prod : product) {
        productNames.add(prod.getName().toLowerCase());
    }
    return productNames.contains(checkName.toLowerCase());
}

If you do not like this approach, you can use the ones given already, and replace equals with equalsIgnoreCase.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.