Splitting a string to compare each word to an arraylist

Question

I need to split a string such as "burger and fries" so I can compare all 3 words to ArrayList filled with common words such as "and" so that I can remove them and be left with just "burger" and "fries" to compare against and list filled with food items. This is what I'm playing around with. The else if block works if i enter burger or fries alone, but I want to be able to say "burger and fries" and check to see if they exist in the menu and return true basically.

tl;dr... How do I split the String item and check each of the words in the string against ArrayLists. public boolean checkItem(String item) {

    // example list
    ArrayList<String> menu = new ArrayList<String>();
    menu.add("burger");
    menu.add("fries");

    // common words to check for
    ArrayList<String> common = new ArrayList(Arrays.asList("and"));

    if (/*check for common words here*/  ) {
        // delete words if any 

    }

    else if (menu.contains(/* item string here */)) { 
        System.out.println("true");
        return true;
    }

    else {
        System.out.println("false");

        return false;
    }
}

Show source
| java   | string   | arraylist   2017-01-04 22:01 1 Answers

Answers ( 1 )

  1. 2017-01-04 22:01

    If you are sure that all the entries on your Menu are single worded or separated by something else then a Space then you can do it by simply splitting the input by Space.

    Also, you should be using Set instead of List to perform the comparisons as it is faster.

    Here is a simple example to get you started:

    private void checkMenu() {
        List<String> commonWords = Arrays.asList("and", "or", "not");
        Set<String> commonSet = new HashSet<>(commonWords);
    
        List<String> menu = Arrays.asList("burger", "fries");
        Set<String> menuSet = new HashSet<>(menu);
    
        String input = "burger and fries";
        String[] tokens = input.split(" ");
        for (int x = 0; x < tokens.length; x++) {
            if (!commonSet.contains(tokens[x]) && menuSet.contains(tokens[x])) {
                System.out.println(tokens[x] + " Exist In Menu!");
            }
        }
    }
    

    If you just want to check whether you have everything on the menu or not then you can simply do something like this:

    private boolean checkMenu() {
        List<String> commonWords = Arrays.asList("and", "or", "not");
        Set<String> commonSet = new HashSet<>(commonWords);
    
        List<String> menu = Arrays.asList("burger", "fries");
        Set<String> menuSet = new HashSet<>(menu);
    
        String input = "burger and fries";
        String[] tokens = input.split(" ");
        for (int x = 0; x < tokens.length; x++) {
            if (!commonSet.contains(tokens[x]) && !menuSet.contains(tokens[x])) {
                System.out.println(tokens[x] + " Doesn't Exists On The Menu!");
                return false;
            }
        }
    
        System.out.println("Everything Exists On The Menu!");
        return true;
    }
    
◀ Go back