Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: How to check if a word is there?

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:

    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) {
     
        } 
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Only getting last word
    By zlloyd1 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 29th, 2013, 07:34 PM
  2. Spell Check Ms Word style
    By Chander in forum Totally Off Topic
    Replies: 1
    Last Post: December 6th, 2012, 04:11 AM
  3. Replies: 5
    Last Post: August 20th, 2012, 01:01 AM
  4. read a file word by word
    By poornima2806 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 23rd, 2012, 03:14 PM
  5. Reading a text file word by word
    By dylanka in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: October 21st, 2011, 02:06 PM