How to use an Array with an If statement

29,677

Solution 1

Hopefully I'm understanding the question correctly. You want to user to enter the words, "one", "two", "three", etc in order, and at each step of a successful entry, play a test sound?

In that 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 you would want to take the same action at each step. If you can describe your requirements for 'correct response' behavior a little more, I can provide a more precise answer.

We use a Queue above, as it exhibits First-In-First-Out ordering. This means that the matches must appear in the order they are added (all the push statements above). Inside the loop, when a successful match occurs, the next desired match will be checked. For instance, with a Queue containing("three", "two", "one") and matches containing ("one", "two", "thirty"), the loop will perform as follows:

  1. match "one" will be compared to the head of the queue, "one"
  2. This matches, so we "pop" the head, leaving ("three", "two") in the queue
  3. The next match, "two" will be compared to the head of the queue (now "two")
  4. This matches, so we again pop the head, leaving ("three") in the queue
  5. The next match, "thirty" will be compared with the head of the queue (now "three")
  6. This does not match, so no further changes occur with the queue

If you want to have specific behavior for each of the matches (i.e., do something when "one" matches, then something else when "two" matches, etc) you could wire up something like the following (in addition to the 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();
  }
}

Solution 2

Basicly what you looking for is an HasMap like this:

Map<String,Integer> map = new HashMap<String,Integer>();
map.put("one",1);
map.put("two",2);

hth

Share:
29,677
Nitan Shalom
Author by

Nitan Shalom

Updated on May 15, 2020

Comments

  • Nitan Shalom
    Nitan Shalom almost 4 years

    I am new to Java. I'm not really sure how to effectively use an array in Java. I may not be able to use the correct terms so I will attempt to show you in 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 would like to set up my if function so that (assuming that arrayCount[1] is the default.... If that array is at that first state of [1], and "one".equals(match) then it sets the array to arrayCount[2] and then from there on. Basically, if "one" = match, it should set arrayCount to 2, if "two" = match AND the first if statement has already been executed, it will play the test sound. Ultimately this chain would go all the way up to 100, but this is just to test.

    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();
    
                    }
    
  • Nitan Shalom
    Nitan Shalom almost 11 years
    This is a reprint of my earlier comment -- Hi guys, thanks for your response. I am using this to accomplish as follows. I'd like the user to input "one" when he inputs "one" it plays a test sound or does whatever. Then I would like the user to input "two" and then it does whatever I decide having it do. But the reason for all of this complication is because I need the user to first input "one" before he can then have access to what happens when he inputs "two" hope you guys now understand it a little bit more clearly. Let me know also how to increment the array by one....
  • Nitan Shalom
    Nitan Shalom almost 11 years
    Almost, I have the way that the user inputs the message all figured out. It's just I'd like the user's input to first be "one" before he has the ability to have the app respond to any other inputs. After the app receives "one" from the user it then looks for "two" and ONLY "two". If the user's input is "two" and he has previously inputed "one", then the actions for "two" will work.
  • Ron Dahlgren
    Ron Dahlgren almost 11 years
    The Queue based solution I posted above will do what you want. That is, it will only check for 'one' until it matches, then it will only check for 'two' until it matches, etc.
  • Ron Dahlgren
    Ron Dahlgren almost 11 years
    Added some hints on how to have specific actions for each match
  • Nitan Shalom
    Nitan Shalom almost 11 years
    Ahhh, common mistake of a beginner for not noticing that. Thanks.
  • Nitan Shalom
    Nitan Shalom almost 11 years
    Also, this is again my beginner side talking, but for some reason I'm getting a inputs.push red line under push.... Can you explain to me what the fix is and how this is being used. Shouldn't I change "LinkedList" to "matches"? I'm also getting a red line under "pop".... Just making this clear here, I'm using SpeechRecognizer to put the results in the array list.
  • Ron Dahlgren
    Ron Dahlgren almost 11 years
    If you have imported java.util.LinkedList and java.util.Queue, you can see in the docs here (docs.oracle.com/javase/6/docs/api/java/util/…) that push adds a new element to the queue. I'm not sure why your IDE is complaining about it. You should not change LinkedList to matches. The LinkedList is storing the things you want to check the user input against - use whatever name you like for it. I tried to adapt the code you posted above. I'm assuming 'matches' is an Iterable<String> of user input, and 'inputs' (perhaps poorly named) is what you want to check against.
  • Nitan Shalom
    Nitan Shalom almost 11 years
  • Nitan Shalom
    Nitan Shalom almost 11 years
    Ok,I'm getting an error that I am unfimiliar with, maybe you have heard of it (again on the push's and pop's, btw, setting the inputs directly to LinkedList seemed to help the last problem) This is what I am getting "Call requires API level 9 (current min is 8): java.util.LinkedList#pop" I'm on the latest API, so not sure why this is happening.
  • Ron Dahlgren
    Ron Dahlgren almost 11 years