首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >编写搜索条件java

问编写搜索条件java
EN

Stack Overflow用户
提问于 2015-12-02 18:15:09
回答 2查看 125关注 0票数 1

我有一个由以下字段组成的对象数组:

  • int userID
  • int accountID
  • 字符串accountHoldersName
  • 字符串accountType
  • 双平衡
  • 字符串accountStatus

在我的程序中,我必须要求用户输入每个字段的搜索条件。如果用户将字段留为空白,则不应将其包括在搜索中。也就是说,如果用户没有指定帐户持有人的名称,那么应该显示所有与帐户持有人名称相匹配的其他条件的帐户。

我不知道如何为这样的比较编写一个if(),在这里我可能没有一个可以比较的值。

代码语言:javascript
复制
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);
    }
}
EN

回答 2

Stack Overflow用户

发布于 2015-12-02 18:24:25

尝试一个OR条件:

代码语言:javascript
复制
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);
}

希望这能帮上忙!

票数 1
EN

Stack Overflow用户

发布于 2015-12-02 18:25:50

您可能需要通过!= null显式地或隐式地进行一些空值检查:

代码语言:javascript
复制
   if (ac3.holdersName.equalsIgnoreCase(holdersN)) { 
      ....
   }

阅读有关String.equalsIgnoreCase()的更多信息,了解它的工作原理。由于在某些情况下使用的是int,所以需要检查它们是否是合理的值,因为它们不能为null。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34050061

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档