How to check if a word is there?
I have a code with an array of words. The program asks the user to type in a word. The program should then search through the array and see if the word exists in the array or not. If it does exist, it will output "word is on the list." If not, then it will say "word is not on the list."
I am unsure as to how I should proceed writing the method to check the word. No clue whatsoever. Nada. If someone could guide me or provide a suggestion as to how I should proceed, that would be appreciated.
This is what I do have so far:
Code :
import javax.swing.JOptionPane;
public class checkWord2 {
public static void main(String[] args) {
String[] wordArray = { "hello", "goodbye", "cat", "dog", "red", "green", "sun", "moon" };
String isOrIsNot, inputWord;
inputWord = JOptionPane.showInputDialog(null, "Enter a word in all lower case:");
if (wordIsThere(inputWord, wordArray))
isOrIsNot = "is";
else
isOrIsNot = "is not";
JOptionPane.showMessageDialog(null, "The word " + inputWord + " " + isOrIsNot + " on the list.");
}
public static boolean wordIsThere(String findMe, String[] theList) {
}
}
Re: How to check if a word is there?
Re: How to check if a word is there?
So after reading through that and thinking... should I use an if statement? Such as if(inputWord.equals(wordArray))? But then I'd have to have some sort of loop that has it check the entire list of words...
Am I going down the right path of thinking here or do I need to be redirected?
Re: How to check if a word is there?
Your starting in the middle and working outwards. Yes, you will use a loop and an if statement.
Re: How to check if a word is there?
But how do you use a loop on a string array? I'm not sure how to use a loop to get it to search the whole array?
Re: How to check if a word is there?
The contents of an array don't change how you use a loop.
See the links in post#2
Re: How to check if a word is there?
for (string item: wordArray) {
if (inputWord.equals(wordArray)
JOptionPane.showMessageDialog(null, "The word " + inputWord + " " + isOrIsNot + " on the list.");
}
Am I getting warmer or colder here? Or has my temperature not changed at all?
Re: How to check if a word is there?
Does the code compile? Add a println statement in the loop to print out each word that is obtained from the array so you can see that all the values are looked at.
When it compiles without error, execute it for testing.
Compile and test freqjuently. Don't wait until it is all type in because there will be too many errors to fix. Compile often and fix the few errors before typing in more code and making more errors.