Cormac Valentine Cormac Valentine -4 years ago 70
Java Question

String to match user input

Is there a way I can it make so if the user makes a search such as "car" or "speakers" then the auction will still be found? Any help would appreciated.

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 :");
}

Answer Source

You should search and check all auctions. And you can print your results like this. Try this one.

    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 :");

If you search for 'a' Result:

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

If you search for 'car'

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

If you search for 'none'

Search::
none
No Match Found :
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download