英文:
How can I use not equals in the get method and if statement
问题
我正在尝试遍历我的客户列表,查看个人号码是否匹配,否则应该抛出一个错误,说明客户不存在。但是即使存在客户,我始终收到客户不存在的错误消息。
我的方法:
public ArrayList<String> getCustomer(String pNo){
ArrayList<String> custInfo = new ArrayList<String>();
for(int i=0; i<customerList.size(); i++){
if(customerList.get(i).getPersonalNumber().equals(pNo)){
custInfo.add(customerList.get(i).getCustomerInfo());
for(int j = 0; j<customerList.get(i).getAllAccounts().size(); j++){
custInfo.add(customerList.get(i).getAllAccounts().get(j).getAccount());
}
return custInfo;
}
if (!customerList.get(i).getPersonalNumber().equals(pNo)) {
JOptionPane.showMessageDialog(null, "There is no such customer", "Error", JOptionPane.PLAIN_MESSAGE);
}
}
return null;
}
当我在第一个if之后写上else,它做的事情是一样的。
英文:
I am trying to loop through my customer list and see if the personal numbers match, otherwise it should throw an error that the customer doesn't exist. But I always get the error message that customer doesn't exist even when it does.
My method:
public ArrayList<String> getCustomer(String pNo){
ArrayList<String> custInfo = new ArrayList<String>();
for(int i=0; i<customerList.size(); i++){
if(customerList.get(i).getPersonalNumber().equals(pNo)){
custInfo.add(customerList.get(i).getCustomerInfo());
for(int j = 0; j<customerList.get(i).getAllAccounts().size(); j++){
custInfo.add(customerList.get(i).getAllAccounts().get(j).getAccount());
}
return custInfo;
}
if (!customerList.get(i).getPersonalNumber().equals(pNo)) {
JOptionPane.showMessageDialog(null, "There is no such customer", "Error", JOptionPane.PLAIN_MESSAGE);
}
}
return null;
}
When I write else after first if it does the same thing
答案1
得分: 3
你正在检查个人号码是否在每次迭代中匹配。即使第一个客户不匹配,您也会显示对话框,即使第二个客户匹配。
由于在 if 语句内部有一个 return
语句,当找到匹配的客户时,执行将退出循环。您只需将 showMessageDialog
移动到循环之后,仅当未找到匹配的客户时才会运行:
public ArrayList<String> getCustomer(String pNo) {
ArrayList<String> custInfo = new ArrayList<String>();
for (int i = 0; i < customerList.size(); i++) {
if (customerList.get(i).getPersonalNumber().equals(pNo)) {
custInfo.add(customerList.get(i).getCustomerInfo());
for (int j = 0; j < customerList.get(i).getAllAccounts().size(); j++) {
custInfo.add(customerList.get(i).getAllAccounts().get(j).getAccount());
}
return custInfo;
}
}
JOptionPane.showMessageDialog(null, "There is no such customer", "Error", JOptionPane.PLAIN_MESSAGE);
return null;
}
英文:
You are checking if the personal number matches on every iteration. If the first customer does not match, you show the dialog, even if the second customer did match.
Since you have a return
statement within the if-statement, the execution exits the loop when a matching customer is found. You can simply move showMessageDialog
until after the loop, and it will run only when a matching customer is not found:
public ArrayList<String> getCustomer(String pNo) {
ArrayList<String> custInfo = new ArrayList<String>();
for (int i = 0; i < customerList.size(); i++) {
if (customerList.get(i).getPersonalNumber().equals(pNo)) {
custInfo.add(customerList.get(i).getCustomerInfo());
for (int j = 0; j < customerList.get(i).getAllAccounts().size(); j++) {
custInfo.add(customerList.get(i).getAllAccounts().get(j).getAccount());
}
return custInfo;
}
}
JOptionPane.showMessageDialog(null, "There is no such customer", "Error", JOptionPane.PLAIN_MESSAGE);
return null;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论