Nitan Shalom
Nitan Shalom

Reputation: 69

How to use an Array with an If statement

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

                }

Upvotes: 3

Views: 51346

Answers (4)

Larry McKenzie
Larry McKenzie

Reputation: 3283

From what I can understand you may want to create an arraylist like this:

String[] array = ["zero", "one", "two"];
ArrayList<String> mArrayList = new ArrayList<String>(Arrays.asList(array));
int index = mArrayList.indexOf(user_input);
//use the index to play the appropriate sound...

Upvotes: 0

user1898811
user1898811

Reputation:

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

Upvotes: 3

BLuFeNiX
BLuFeNiX

Reputation: 2594

"If that array is at that first state of [1] ... it sets the array to arrayCount[2]" doesn't make sense. Arrays do not have "states".

An array is an easy way to make a lot of variable or objects of the same type. Instead of declaring 5 int variables like this:

int num1 = 234;
int num2 = 635;
int num3 = 3568;
int num4 = 23;
int num5 = 745;

You can do this:

int[] nums = {234, 635, 3568, 23, 745};

And then you can reference them like this:

System.out.println(nums[2]);

This would print the number 3568 (since the array is 0-based, nums[0] would be 234).

Please explain what you want to accomplish (try not to use code in your description), and I will give you some code that does what you want.

Upvotes: 1

Kitesurfer
Kitesurfer

Reputation: 3561

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

Upvotes: 1

Related Questions