如果用户进行诸如“汽车”或“扬声器”之类的搜索,我是否可以这样做,那么仍然可以找到拍卖?任何帮助将不胜感激。
private static void 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 :");
}
答案 0 :(得分:0)
你可以尝试:
make_index_sequence
答案 1 :(得分:0)
您可以编写一个执行不区分大小写的比较并调用它的方法,例如:
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();
}
答案 2 :(得分:0)
您应该搜索并检查所有拍卖。你可以像这样打印你的结果。 试试这个。
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 :");
如果您搜索&#39; a&#39; 结果:
---Browse Auction---
Search::
a
Auction Found : Car - Honda Civic VTI 1.8 Petrol
Auction Found : Sony Bluetooth Speakers
Auction Found : auctionObject
如果您搜索&#39; car&#39;
---Browse Auction---
Search::
car
Auction Found : Car - Honda Civic VTI 1.8 Petrol
如果您搜索“无”&#39;
Search::
none
No Match Found :