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!

1

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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();
}

4 Comments

Right, that's why I am asking if there are any other ways to do it?
in this case, if I enter "test", I will get an infinite loop and it won't except the correct input
That's because it should be scan.nextLine() and not just scan.next(). It works perfectly. Check here: ideone.com/kB4U3n
@S.Anthony Your Welcome! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.