string - Java startsWith dont work properly -


I found a table of two stars:

  string [] tab = {"why ","Why"};  

And I want to see if someone is asking a question from my tab []:

  for (int I = 0; i & lt; tab.length; i ++) {if!! Message.startsWith (tab [i]) {System.out.println ("why or why ask"); breakage;} }  

When I'm typing my input: "Why is the weather bad?", It asks: "Why or why." Also, when I type I am: "How are you?", Ask "why or why". "

This program only wants to allow the question which is why and why it starts.

What am I doing wrong?

What are you doing:

  1. match against first entry
  2. if it does not match Show the message to the user and cancel.

What do you want to do:

  1. Mail
  2. If it does not match, Try the next entry.
  3. If Do Oi entry does not match, then show the user messages and set aside.

Here's an example:

  Boolean found = false; For (int i = 0; i & lt; tab.length; i ++) {if (with message.starts (tab [i])) {found = true; break; }} If ((found!) {System.out.println ("why or why ask it");}  

In your special example, you can only check the lowercase version of the string Can:

  if (! Message.toLowerCase (). "Why")) {System.out.println ("Why or Why to Ask"); }  

Comments