我有一个由以下字段组成的对象数组:
在我的程序中,我必须要求用户输入每个字段的搜索条件。如果用户将字段留为空白,则不应将其包括在搜索中。也就是说,如果用户没有指定帐户持有人的名称,那么应该显示所有与帐户持有人名称相匹配的其他条件的帐户。
我不知道如何为这样的比较编写一个if(),在这里我可能没有一个可以比较的值。
String acT;
String holdersN;
String acStat;
int usrId = 0, acId = 0;
double bal = 0;
System.out.println("SEARCH MENU:\n");
System.out.print("Account ID : ");
acId = sc.nextInt();
System.out.print("User ID: ");
usrId = sc.nextInt();
System.out.print("Balance: ");
bal = sc.nextDouble();
System.out.print("Holders Name : ");
holdersN = sc.nextLine();
System.out.print("Type (Savings Current): ");
acT = sc.nextLine();
sc.nextLine();
System.out.print("Status: ");
acStat = sc.nextLine();
System.out.println("\n===== SEARCH RESULTS =====\n");
System.out.println("Account ID\tUser ID\tHolders Name\tType\tBalance\tStatus");
// accountsInfo is my ArrayList
for(int j = 0;j < accountsInfo.size(); j++)
{
AccountInformation ac3 =(AccountInformation)accountsInfo.get(j);
if( // i dont know how to write search criteria here which will compare only those values that are provided by user)
{
System.out.println(ac3.accountID +"\t"+ ac3.userID +"\t"+ ac3.holdersName +"\t"+ ac3.accountType +"\t"+ ac3.balance +"\t"+ ac3.accountStatus);
}
}发布于 2015-12-02 18:24:25
尝试一个OR条件:
if((acId == -1 || ac3.accountID == acId) && (acStat == null/*Empty string*/ || acStat.equals(ac3.accountStatus)) && (so on)) {
System.out.println(ac3.accountID +"\t"+ ac3.userID +"\t"+ ac3.holdersName +"\t"+ ac3.accountType +"\t"+ ac3.balance +"\t"+ ac3.accountStatus);
}希望这能帮上忙!
发布于 2015-12-02 18:25:50
您可能需要通过!= null显式地或隐式地进行一些空值检查:
if (ac3.holdersName.equalsIgnoreCase(holdersN)) {
....
}阅读有关String.equalsIgnoreCase()的更多信息,了解它的工作原理。由于在某些情况下使用的是int,所以需要检查它们是否是合理的值,因为它们不能为null。
https://stackoverflow.com/questions/34050061
复制相似问题