To make things simple, I have an ArrayList called "items" (which stores a string the user inputs) and this bit of code:
int index = items.lastIndexOf("i am");
if (index >= 0)
System.out.println("yay");
When I test it out, if I type in "i am" at the beginning it outputs "yay" like it should. But, if I type in something like "yes i am" it doesn't output anything. Is there a way I can have it so "yay" is displayed if I type in something like "yes i am" or am I restricted to having it just at the beginning?
Here's how I setup the ArrayList:
List<String> items = Arrays.asList(user.split("\\s*,\\s*"));
Thanks.
It seems to me you are splitting the input string on a comma (in an awkward way too). Since yes I am
doesn't contain a comma, your array will only have a single element: ["yes I am"]
. Which means that lastIndexOf("i am")
will not match. Hence you aren't seeing yay
being printed out.
Now, to the matter of how to make this work.
yes, i am
user.indexof("i am")
.Basically, it's hard to recommend a solution because I don't know what your ultimate goal is.