Issue
I am actually a newbie to Java and was trying to do a small project. So, in my project, I want to allow the user to enter a sentence, and I want the program to search for particular words in the sentence and give outputs based on that. I use NetBeans to develop my applications.
My code is something like this
String Sentence=jTextField1.getText();
if (Sentence.equals("Hello")
{
jTextField2.setText("Hello was found");
}
else if (Sentence.equals("Donkey")
{
jTextField2.setText("Donkey was found");
}
I know that this code makes no sense and wont run, but I put it so that people can get a general idea of what I am trying to achieve.
Please help me.
Solution
Use .contains
like so:
String Sentence = jTextField1.getText();
if (Sentence.contains("Hello")) {
jTextField2.setText("Hello was found");
} else if (Sentence.contains("Donkey")) {
jTextField2.setText("Donkey was found");
}
Answered By - Spectric
Answer Checked By - Gilberto Lyons (JavaFixing Admin)