-4

Trying to make an if statement for a game where I give the user a scrambled word and they have to input a word made up of those scrambled letters. I have hardcoded three answers they can use. I'm stuck on trying to make an IF else statement to tell the user if they got the answer correct or to try again.

import java.util.*; 
public class Scramble {

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        String[] wordbank = new String [10];
        int answer; 
        wordbank[0] = "GALEE";
        wordbank[1] = "OLWBE"; 
        wordbank[2] = "RHCIA"; 
        wordbank[3] = "RWSIT"; 
        wordbank[4] = "NTARI"; 
        wordbank[5] = "ETLBA"; 
        wordbank[6] = "TIRSH"; 
        wordbank[7] = "TLUFE"; 
        wordbank[8] = "MIGEIN"; 
        wordbank[9] = "RAEHT"; 
        String[] GALEE = {"Eagle", "Gale", "Leg"};
        String[] OLWBE = {"Elbow", "Below", "Lobe"}; 
        String[] RHCIA = {"Chair", "Hair", "Air"}; 
        String[] RWSIT = {"Wrist", "Wit", "It"}; 
        String[] NTARI = {"Train", "Rant", "Art"}; 
        String[] ETBLA = {"Table", "Late", "Bet"}; 
        String[] TIRSH = {"Shirt", "Sir", "Sh"}; 
        String[] TLUFE = {"Flute", "Felt", "let"}; 
        String[] MIGEIN = {"Gemini", "Mini", "Gem"}; 
        String[] RAEHT = {"Heart", "Hate", "Ear"}; 

        System.out.println("Create a five letter word out of " + wordbank[0] + ".");
        answer = s.nextInt();
        if (answer) { 

        }
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
  • 3
    Note that `nextInt()` reads an `int` value, you probably want to read a `String` (i.e `next()`) . – Arnaud Jan 16 '19 at 13:43
  • 2
    Possible duplicate of [How do I determine whether an array contains a particular value in Java?](https://stackoverflow.com/questions/1128723/how-do-i-determine-whether-an-array-contains-a-particular-value-in-java) –  Jan 16 '19 at 13:44
  • You want to check if the value (given by the player) is in the array (answers you hard coded). See [this](https://stackoverflow.com/questions/1128723/how-do-i-determine-whether-an-array-contains-a-particular-value-in-java) – vincrichaud Jan 16 '19 at 13:45

3 Answers3

1

As noted in the comment by Arnaud, you should probably use next(), at least check that the value of answer is really the user input.

Once this is out of the way, you will want to check that answer is one of the three acceptable solutions.

You can use this in java 8:

boolean resultOk = Arrays.asList(GALEE).contains(answer);
if (resultOK) {
    // do something
}

This will check that answer belongs to your GALEE array (so it has to be equal to either eagle, gale or leg.

Tom
  • 1,357
  • 2
  • 13
  • 32
0

Assuming that the user types the word, you need to read it with next() instead of nextInt().

The if statement reads a boolean, that boolean in this case should come from a comparison (for example, equals as a simple solution).

if(answer.equals("Eagle")){
  //do something
}

See How do I compare strings in Java? for more info

Or, for a better solution you should try to check in only one step if the answer given by the user matches some element of the array

if(Arrays.asList(GALEE).contains(answer)){
  //do something
}

For this you can check How do I determine whether an array contains a particular value in Java?

brunorey
  • 2,135
  • 1
  • 18
  • 26
  • I have to note that this will get real messy real fast. What if the wordbank is 100 words? –  Jan 16 '19 at 13:50
0

You can simply iterate through GALEE to see if what they have entered is equal to one of the words in GALEE. This is what your code should look like:

String answer = scanner.nextLine();
boolean matches = false;

for(String combination: GALEE){
    if(answer.equals(combination)){
        matches = true;
        break
    }
}
if(matches){
    System.out.println("You got it correct!");
} else{
    System.out.println("You got it wrong!");
}

I'd also like to note that you asked them to

Create a five letter word

but some of the words in GALEE are less than 5 letters long.

Ishaan Javali
  • 1,711
  • 3
  • 13
  • 23