2

I am doing "Rock,Paper,Scissors" game and I want to check if the input matches with any arrays, for now I have this code and it works perfect:

String[] rockArray = {"Rock","rock","1"},
         paperArray = {"Paper","paper","2"},
         scissorsArray = {"Scissors","scissors","3"};
String[][] answersArray = {rockArray, paperArray, scissorsArray};

//Scanner
Scanner scan = new Scanner(System.in);

// Input
System.out.print("\nRock, Paper or Scissors? -> ");
String answer = scan.next();

 // Checking if the input contains the right values
 while(!Arrays.asList(answerRock).contains(answer) &&
       !Arrays.asList(answerPaper).contains(answer) &&
       !Arrays.asList(answerScissors).contains(answer)) {

         System.out.print("Try again: ");
         answer = scan.next();
 }

But I want to make it simple and here is my idea but for some reason it doesn't work:

// Here is my arrays 
String[] rockArray = {"Rock","rock","1"},
         paperArray = {"Paper","paper","2"},
         scissorsArray = {"Scissors","scissors","3"};
String[][] globalArray = {rockArray, paperArray, scissorsArray};

// Input
System.out.print("\nRock, Paper or Scissors? -> ");
String answer = scan.next();

//Validation
while(!Arrays.asList(globalArray).contains(answer) {
      System.out.println("Try again: ");
      answer = scan.next();
}

Thank you very much!

John E.
  • 321
  • 4
  • 11

3 Answers3

3
String[][] globalArray = {rockArray, paperArray, scissorsArray};
...
Arrays.asList(globalArray)

This call to Arrays.asList() will create list with 3 elements, the 3 array and not a list with the content of the arrays. When you then use contains to search for the element, you will always get false because answer will never be on of the arrays (how would it, it's a String not an array).

I suggest you do something like this:

List<String> allAnswers = Arrays.asList(rockArray);
allAnswers.addAll(Arrays.asList(paperArray));
allAnswers.addAll(Arrays.asList(scissorsArray));
...
while(!allAnswers.contains(answer)) {
  ...

This also saves you from calling Arrays.asList() over and over again which improves performance.

MartinS
  • 2,759
  • 1
  • 15
  • 25
1

!Arrays.asList(globalArray).contains(answer) doesn't work because the Arrays.asList(...) method will output a List<String[]> not List<String>.

If you really want to use asList(...) you have to input an String[] allPossibleInputString

Maljam
  • 6,244
  • 3
  • 17
  • 30
0

Your second code snippet does not work because you can't do something like this !Arrays.asList(globalArray).contains(answer).

One solution is to copy the contents of the three array into your global array like this:

String[] rockArray = {"Rock","rock","1"},
         paperArray = {"Paper","paper","2"},
         scissorsArray = {"Scissors","scissors","3"};
String[] globalArray = new String[9];
/* Copy Contents To Global Array */
System.arraycopy(rockArray, 0, globalArray, 0, 3);
System.arraycopy(paperArray, 0, globalArray, 3, 3);
System.arraycopy(scissorsArray, 0, globalArray, 6, 3);

Now you can do this:

while(!Arrays.asList(globalArray).contains(answer)) {
    System.out.println("Try again: ");
    answer = scan.nextLine();
}
user2004685
  • 9,548
  • 5
  • 37
  • 54