Java – How to use arrays in If statements

How to use arrays in If statements… here is a solution to the problem.

How to use arrays in If statements

I’m new to Java. I’m not quite sure how to use arrays efficiently in Java. I may not be able to use the correct terminology, so I’ll try to show you with code.
Basically, this is my array.

int[] arrayCount = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};

I want to set my if function so that (assuming arrayCount[1] is the default…) If the array is in the first state of [1] and “one”.equals(match) then it sets the array to arrayCount[2] and starts from there. Basically, if “one” = match, it should set the arrayCount to 2, and if “two” = match and the first if statement has been executed, it will play a test sound. Eventually this chain will go all the way up to 100, but that’s just for testing.

for (String match : matches) {
                if (arrayCount[1]== 1 && "one".equals(match)) {
                    testSound.start();
                    arrayCount[2]=2; 
                } else if (arrayCount[2]==2 && "two".equals(match)) {
                    testSound.start();

}

Solution

Hopefully I understand the issue correctly. Do you want users to enter the words “one”, “two”, “three”, etc. sequentially and play a test sound at each step of successful input?

In this case, consider the following:

import java.util.Queue;
import java.util.LinkedList;

Queue<String> inputs = new LinkedList<String>();
inputs.push("one");
inputs.push("two");
inputs.push("three");
 etc
 Then to check the user input
for (String match : matches) {
  if (match.equals(inputs.peek())) {
    inputs.pop();  Removes the element you just matched
    testSound.start();
  }
}

Note that this assumes that you want to do the same in each step. If you could describe in more detail what you are asking for the “right response” behavior, I can provide a more accurate answer.

We use the queue above because it shows First-In-First-Out Order. This means that matches must appear in the order in which they were added (all push statements above). Inside the loop, when a match succeeds, the next desired match is checked. For example, for a queue containing (“three”, “two”, “one”) and a match containing (“one”, “two”, “thirty”), the loop would perform as follows:

    Matching “one”

  1. will be compared to the head “one” of the queue
  2. This matches, so we “pop” the head, leaving (“three”, “two”) in the queue
  3. In the next game, “two” will be compared to the head of the queue (now “two”
  4. ).

  5. This matches, so we pop the head again, leaving (“three”) in the queue
  6. In the next game, “Thirty” will be compared to the head of the team (now “Three”
  7. ).

  8. This does not match, so there are no further changes to the queue

If you want to have a specific behavior for each match (i.e., do something when “one” matches, then do something else when “two” matches, etc.), you can connect like this (except above).

public interface MatchAction {
  public void doTheThing();
}

Map<String, MatchAction> actionMap = new HashMap<String,MatchAction>();
 Fill this bad boy up
actionMap.put("one", new MatchAction() { public void doTheThing() { /* do stuff */ } });
 Etc for each action (you can reuse instances here if some actions are the same)
 Then, we modify the check above to be:
for (String match : matches) {
  if (match.equals(inputs.peek())) {
    String input = inputs.pop();
    MatchAction action = actionMap.get(input);
    if (action != null) action.doTheThing();
  }
}

Related Problems and Solutions