【问题标题】:String to match user input匹配用户输入的字符串
【发布时间】:2017-08-15 06:36:12
【问题描述】:

我有什么办法可以做到,如果用户进行“汽车”或“扬声器”等搜索,那么仍然会找到拍卖?任何帮助将不胜感激。

私有静态无效browseAuction() {

    System.out.println("---Browse Auction---");

    String[] auctions = {"Car - Honda Civic VTI 1.8 Petrol", "Sony Bluetooth Speakers", "auctionObject" };

    java.util.List<String> list1 = java.util.Arrays.asList(auctions);


    System.out.println("Search::");
    String searchWord = S.next();

    if (list1.contains(searchWord)) {
        System.out.println("Auction Found : ");
        java.util.Arrays.toString(auctions);
    }else {
        System.out.println("No Match Found :");
    }

【问题讨论】:

  • 或者以某种方式显示所有拍卖的列表,这样会好很多。

标签: java string list search


【解决方案1】:

您应该搜索并检查所有拍卖。你可以像这样打印你的结果。 试试这个。

    Scanner S = new Scanner(System.in);
    boolean found = false;
    String[] auctions = {"Car - Honda Civic VTI 1.8 Petrol", "Sony Bluetooth Speakers", "auctionObject" };
    java.util.List<String> list1 = java.util.Arrays.asList(auctions);

    System.out.println("---Browse Auction---");
    System.out.println("Search::");
    String searchWord = S.next().toLowerCase();

    //Searching all auctions for 'searchWord' key.
    for (int i = 0; i < list1.size(); i++){
        String currentAuction = list1.get(i).toLowerCase();
        if (currentAuction.contains(searchWord)) {
            System.out.println("Auction Found : " + list1.get(i));
            found = true;
        }
    }

    //There is no match for 'searchWord' key.
    if (!found)
        System.out.println("No Match Found :");

如果您搜索“a” 结果:

---Browse Auction---
Search::
a
Auction Found : Car - Honda Civic VTI 1.8 Petrol
Auction Found : Sony Bluetooth Speakers
Auction Found : auctionObject

如果您搜索“汽车”

---Browse Auction---
Search::
car
Auction Found : Car - Honda Civic VTI 1.8 Petrol

如果您搜索“无”

Search::
none
No Match Found :

【讨论】:

  • 没问题,不要忘记将我的评论标记为真实答案,谢谢
  • 别担心,伙计刚刚做了,我有点困惑如何让我的 placeAuction 方法中制作的拍卖对象成为搜索的一部分。再次感谢任何帮助。
  • 我完全不理解你。您想搜索“auctionObject”的关键字吗?
  • 是的,在我的 placeBid 方法中,用户可以创建拍卖。我希望这次拍卖包括搜索。对不起,如果我不是很清楚。编程还是新手
  • 当用户创建新的拍卖时,您应该将此拍卖添加到“list1”列表中。伪示例:list1 = [汽车拍卖,房屋拍卖] >> 用户在 placeBid 功能上创建名称为“板拍卖”的新拍卖。 >> 将“板拍卖”添加到 list1。 >> new list1 =[汽车拍卖、房屋拍卖、板拍卖] >> 可以再次使用 list1 进行搜索
【解决方案2】:

你可以试试:

if (list1.toLowerCase().contains(searchWord.toLowerCase())) {
        System.out.println("Auction Found : ");
        java.util.Arrays.toString(auctions);
    }else {
        System.out.println("No Match Found :");
    }

【讨论】:

    【解决方案3】:

    您可以编写一个执行不区分大小写比较的方法并调用它,例如:

    public boolean containsIgnoreCase(List<String> elements, String searchString){
        for(String element : elements){
            if(element.toLowerCase().contains(searchString.toLowerCase())){
                return true;
            }
        }
        return false;
    }
    

    在您的代码中:

    if (containsIgnoreCase(list1,searchWord)) {
        System.out.println("Auction Found : ");
        java.util.Arrays.toString(auctions);
    }else {
        System.out.println("No Match Found :");
    }
    

    这是另一个使用 Java 8 的 stream 的示例:

    public boolean containsIgnoreCase(List<String> elements, String searchString){
        return elements.stream()
                .filter(e -> e.toLowerCase().contains(searchString.toLowerCase()))
                .findFirst()
                .isPresent();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-04
      • 2017-03-19
      • 1970-01-01
      • 2012-11-24
      • 1970-01-01
      • 2019-04-03
      • 2022-08-23
      相关资源
      最近更新 更多