0

I'm searching through a table for a customer with a matching name, if found then I want to return that customer, otherwise return null. The problem is I'm getting null all the time, I think my problem is with the if in the inner for loop.

public class testter {

    public Customer isCustomerInTable(Customer[][] table, String customerName) {
        for ( int r = 0; r < table.length; r++ ) {
            for ( int c = 0; c < table[r].length; c++ ) {
                if ( table[r][c].equals(customerName) ) {
                    return table[r][c];
                }
            }
        }
        return null;
    }
}

My customer class:

class Costumer {

    private String name;

    public Customer()  {
        name = "";
    }

    public void setCostumerName(String name) {
        this.name = name;
    }

    public String getCostumerName(){
        return name;
    }
}

Any ideas? Thanks

2 Answers 2

6

You if condition should be

if (table[r][c].getCostumerName().equals(customerName))

You need to compare the names, but in your case, you are comparing the Customer object with the String customerName.

Sign up to request clarification or add additional context in comments.

1 Comment

As soon as I finished submitting the question I got the answer by my self, but since yours is exactly what I had in mind. I'm gonna give you credits, Thanks
1

You are checking Customer object with a string. That's the reason. You should do the following

if ( table[r][c].getCostumerName().equals(customerName) ) {
    return table[r][c];
}

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.