Results 1 to 4 of 4
- 09-14-2011, 07:01 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 2
- Rep Power
- 0
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 07:07 PM.
- 09-14-2011, 07:29 PM #2
Member
- Join Date
- Sep 2011
- Location
- Athens Greece
- Posts
- 29
- Rep Power
- 0
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! :)
- 09-14-2011, 07:38 PM #3
Member
- Join Date
- Sep 2011
- Location
- Mumbai, India
- Posts
- 35
- Rep Power
- 0
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; } } }
- 09-14-2011, 08:07 PM #4
Member
- Join Date
- Sep 2011
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Lettings a user add new values to an array.
By xquzive in forum New To JavaReplies: 3Last Post: 04-09-2011, 11:53 AM -
need to input values from a text file into an array and count values
By pds8475 in forum New To JavaReplies: 14Last Post: 01-22-2011, 02:36 PM -
user input array
By localhost in forum New To JavaReplies: 5Last Post: 12-30-2010, 04:00 AM -
Loop through array and check values...
By Spinalkord in forum New To JavaReplies: 1Last Post: 10-24-2010, 10:17 AM -
[SOLVED] How to read a file and compare Array values
By DonCash in forum Advanced JavaReplies: 2Last Post: 04-02-2008, 02:22 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks