2013-06-03 52 views
0

我正在通过表匹配名称匹配的客户,如果找到,那么我想返回该客户,否则返回null。问题是我一直都是空的,我认为我的问题是在if for循环中。Java - 在表中搜索匹配字符串的对象

公共类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; 
    } 
} 

我的客户类:

任何想法?由于

回答

6

if条件应该是

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

您需要比较names,但在你的情况,你是比较与String customerNameCustomer对象。

+2

只要我完成提交的问题我得到了我自己的答案,但因为你的正是我的想法。我会给你信用,谢谢 –

+1

很高兴帮助! :) – SudoRahul

1

您正在检查Customer对象与string。这就是原因。你应该做以下的事情

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