Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2011
    Posts
    2
    Rep Power
    0

    Question How can I check/ compare the user input to the values in my array?

    Greetings!

    I'm new to Java and I'm having trouble completing my program. It's supposed to act like the Text Twist game. My next step would be to check whether the user input in myString is equal to the values in the arWords and to give 1pt. for every equal word but I'm not sure how to go about the next step. Can I use the if and else command for it? This is the part of my program that works so far:


    import java.util.*;
    public class TextTwist
    {
    static Scanner kahil = new Scanner (System.in);
    public static void main(String[] args)
    {


    String[][] arWords = {{"ALPHABET", "pal", "lap", "hat", "bet", "eat"},
    {"CHRISTMAS", "hit", "mat", "sit", "sat", "hat"},
    {"WONDERFUL", "one", "red", "few", "rod", "elf"}};

    int random = (int) (Math.random() * 3);

    int sum = 0, counter = 0;

    System.out.println("WORD GAME");
    System.out.println("Form/Input five 3-letter words from the word: " + arWords[random][0]);

    String[] myString = new String[5];
    myString[0] = kahil.next();
    myString[1] = kahil.next();
    myString[2] = kahil.next();
    myString[3] = kahil.next();
    myString[4] = kahil.next();

    }

    }
    Last edited by mindofastudent; 09-14-2011 at 08:07 PM.

  2. #2
    DeNiS_M is offline Member
    Join Date
    Sep 2011
    Location
    Athens Greece
    Posts
    29
    Rep Power
    0

    Default Re: How can I check/ compare the user input to the values in my array?

    You colud use a for loop to traverse the arWords array
    and an inner loop to traverse the myString array.
    In each iteration just compare the values of the arrays!

    Hope i helped! :)

  3. #3
    Join Date
    Sep 2011
    Location
    Mumbai, India
    Posts
    35
    Rep Power
    0

    Default Re: How can I check/ compare the user input to the values in my array?

    Something like this........i know this is not a great code but i have just tried and sorry if there are any mistakes because i am also new to Java and it would be great if i would know my mistake
    Java Code:
    for(int i=0;i<arWords.length;i++)
    {  
        for(int j=0;j<arWords[i].length;j++)
        {
            String s=arWords[i][j];
            boolean b=true;
            while(b)
           {
                  for(int k=0;k<myString.length;k++) {
                   if(s.equals(myString[k]))
                   {
                        "Equal"
                   }
                   else
                         "NOT EQUAL"
                                                                  }
                    b=false;
             }
     }
    }

  4. #4
    Join Date
    Sep 2011
    Posts
    2
    Rep Power
    0

    Default Re: How can I check/ compare the user input to the values in my array?

    Thank you! They both helped!

Similar Threads

  1. Lettings a user add new values to an array.
    By xquzive in forum New To Java
    Replies: 3
    Last Post: 04-09-2011, 12:53 PM
  2. Replies: 14
    Last Post: 01-22-2011, 03:36 PM
  3. user input array
    By localhost in forum New To Java
    Replies: 5
    Last Post: 12-30-2010, 05:00 AM
  4. Loop through array and check values...
    By Spinalkord in forum New To Java
    Replies: 1
    Last Post: 10-24-2010, 11:17 AM
  5. [SOLVED] How to read a file and compare Array values
    By DonCash in forum Advanced Java
    Replies: 2
    Last Post: 04-02-2008, 03:22 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •