This is a code I'm having trouble using any type of loop. I'm fairly new to java, but towards the bottom when running the program; even if the answer you entered was right and matched the one of the words listed, the code would still return "Word is not there". Basically, I'm trying to make it test wether the word entered is there and if it isn't to respond accordingly. Hopefully, I explained clearly and the code typed out is clear as well. Towards the bottom where i say return false and return true is really where I need help. import javax.swing.JOptionPane; public class Lab4 { privatestaticfinalStringinputWord = null; publicstaticvoidmain(String[]args){ String[]wordArray={"hello","goodbye","cat","dog","red","green","sun","moon"}; StringisOrIsNot,inputWord; // This line asks the user for input by popping out a single window // with text input inputWord=JOptionPane.showInputDialog(null, "Enter a word in all lower case:"); // if the inputWord is contained within wordArray return true if(wordIsThere(inputWord, wordArray)) isOrIsNot="is";// set to is if the word is on the list else isOrIsNot="is not";// set to is not if the word is not on the list // Output to a JOptionPane window whether the word is on the list or not JOptionPane.showMessageDialog(null, "The word " + inputWord + " " + isOrIsNot + " on the list."); }//main @SuppressWarnings("unused") publicstaticbooleanwordIsThere(StringfindMe,String[]theList){ returnfalse; if(inputWord == String[]) { returntrue; } } // wordIsThere } // class Lab4Program1
This is a code I'm having trouble using any type of loop. I'm fairly new to java, but towards the bottom when running the program; even if the answer you entered was right and matched the one of the words listed, the code would still return "Word is not there". Basically, I'm trying to make it test wether the word entered is there and if it isn't to respond accordingly. Hopefully, I explained clearly and the code typed out is clear as well. Towards the bottom where i say return false and return true is really where I need help.
import javax.swing.JOptionPane;
public class Lab4 {
privatestaticfinalStringinputWord = null;
publicstaticvoidmain(String[]args){
String[]wordArray={"hello","goodbye","cat","dog","red","green","sun","moon"};
StringisOrIsNot,inputWord;
// This line asks the user for input by popping out a single window
// with text input
inputWord=JOptionPane.showInputDialog(null, "Enter a word in all lower case:");
// if the inputWord is contained within wordArray return true
if(wordIsThere(inputWord, wordArray))
isOrIsNot="is";// set to is if the word is on the list
else
isOrIsNot="is not";// set to is not if the word is not on the list
// Output to a JOptionPane window whether the word is on the list or not
JOptionPane.showMessageDialog(null, "The word " + inputWord + " " + isOrIsNot + " on the list.");
}//main
@SuppressWarnings("unused")
publicstaticbooleanwordIsThere(StringfindMe,String[]theList){
returnfalse;
if(inputWord == String[]) {
returntrue;
}
}
// wordIsThere
} // class Lab4Program1
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 2 images