0

Essentially, I've put a switch statement in a for loop. It has not been working as intended. I think it has something to with the break statement.

What I want the loop to do is go to the first element in the array list, execute code based on the 'action' String, then go to the next until the list ends. However, it seems that whenever I run the program it only executes the code in the first index. Here is some code for the BattleManager class:

Scanner input = new Scanner(System.in);
String cmd;
String[] dtct;
Arraylist<BattleAction> turns = new ArrayList<BattleAction>();



public void handle() {
cmd = input.nextLine();
dtct = (cmd.split());

switch(dtct[0]) {
case "attack":

turns.add(new BattleAction("playerattack"));
getenemyaction();
turnsequence();  


break;

case "defend":
getenemyaction();
turnsequence()
    }
}


public void getenemyaction() {

turns.add(new BattleAction("enemyattack"));
}

public void turnsequence() {

Collections.sort(turns);
print("Size: " + turns.size())
for (int i = 0; i != turns.size(); i ++) {

switch(turns.get(i).action) {

case "playerattack":
playerattack();
break;

case "enemyattack":
enemyattack();
break;
  }

 }
}

public void playerattack() {

print("Player attack");
}


public void enemyattack() {
print("Enemy attack");
}

When I run the program here is the output I am getting with the attack command:

attack
Size: 2
Player Attack

However when I use the defend command the output is:

defend
Size: 1
Enemy Attack

The desired output would be for turnsequence() to go through turns and do check action for the desired code to be executed, like this:

attack
Size: 2
Player Attack
Enemy Attack

I have done my best to cut out as much unnecessary information at possible to prevent confusion. Thank you so much for reading my post! I really appreciate your help!

Bread Bouquet
  • 23
  • 1
  • 1
  • 10
  • Can't see the issue from the posted code. Inside your `for` loop at the top, I'd add: `print(i + ": " + turns.get(i).action);` to see if you can spot the error. – Idle_Mind Nov 23 '22 at 17:52
  • 1
    I agree with Idle_Mind, also you may have cut out _too much_ information — are `input`, `cmd`, and `dtct` class member variables in BattleManager? The next line `Arraylist = new ArrayList();` you've dropped the variable name ... can I assume that one is `turns`? and please fix the indentation and line spacing in the question. – Stephen P Nov 23 '22 at 17:57
  • Idle_Mind, I have tried adding a print statement in the for loop outside of the switch, but when I do I get 2 different errors. `Cannot find symbol: i` `switch(turns.get(i).action)` and this: `illegal parenthesized expression:` ``switch(turns.get(i).action)`, if you want I can try to put all the code up somewhere but it's on a VM and I would have difficulty transferring it. – Bread Bouquet Nov 23 '22 at 18:18
  • And you put that inbetween the `for` line and the `switch` line? – Idle_Mind Nov 23 '22 at 18:24
  • Do you by chance have a semi-colon `;` after the right parenthesis `)` of your `for` loop, before the left bracket `{`? Like this? `for (int i = 0; i != turns.size(); i ++); {` – Idle_Mind Nov 23 '22 at 18:27
  • I doubt posting *all* of the code is a good idea. Instead, are you able to create and post a [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) ? – Old Dog Programmer Nov 24 '22 at 01:48
  • Idle_Mind, I think this was the issue, the code compiled fine so I guess I didn't notice it. This was driving me crazy, sorry for not noticing – Bread Bouquet Nov 24 '22 at 18:32
  • No worries! My Computer Science students do this all the time. Drives them nuts, too. Compiles fine, and is super hard to spot! You can also make this mistake with an `if` and a `while` statement... – Idle_Mind Nov 24 '22 at 19:09
  • It's called a "null statement" or "empty statement". https://stackoverflow.com/questions/32593218/why-does-java-have-an-empty-statement – Old Dog Programmer Nov 24 '22 at 20:48
  • Since the problem was resolved without an answer, consider deleting the question. – Old Dog Programmer Nov 25 '22 at 01:08
  • No, I should definitely get infinite points for correctly deducing the problem without it actually being posted in the question. ;) – Idle_Mind Nov 25 '22 at 01:10

0 Answers0