0

So I want to make a word matching game like crosswords, match the word and other puzzle game in my Java Netbeans. I have had trouble making my int = the words I store in my array.

String word; int numword=0;
    // Words that will be stores in my arry
    String [] myNames = {"Pie","Soccer","Chelsea","Gaming","Steam"};
    // User inputs the number
    numword = new Scanner(System.in).nextInt();
    // Print the word for testing purposes
    System.out.println(myNames[numword]);
    // Type the word 
    word = new Scanner(System.in).nextLine();
    // Check if the word is right
    if (word.equals(n)){
        System.out.println("You got it");

    }else{
        System.out.println("You got it wrong");
    }
C.Ikongo
  • 1,706
  • 1
  • 16
  • 15

2 Answers2

0

I've made little modification to your code. Is this what you want?

String word;
int numword = 0;
Scanner scanner = new Scanner(System.in);
// Words that will be stores in my arry
String[] myNames = {"Pie", "Soccer", "Chelsea", "Gaming", "Steam"};
// User inputs the number
numword = scanner.nextInt();
while (numword >= myNames.length || numword < 0) {
    System.out.println("Not a valid number, enter value between 0 and " + (myNames.length - 1));
    numword = scanner.nextInt();
}
// Print the word for testing purposes
System.out.println(myNames[numword]);
// Type the word
word = scanner.next();
// Check if the word is right
if (word.equals(myNames[numword])) {
    System.out.println("You got it");
} else {
    System.out.println("You got it wrong");
}
Lahiru Ashan
  • 767
  • 9
  • 16
0

You can use a BufferedReader to get the users input:

import java.util.BufferedReader;

public class Main {
    public static void main (String[] args) {
        BufferedReader br = new BufferedReader (new InputStreamReader (System.in)));
        String input = br.readLine ();
    }
}