Skipping To The Next if Statement from Inside a for Loop

Tags: , , ,



I’ve been learning some Java in my spare time and I’m a beginner, so I’m very sorry if I don’t understand some simple concepts. I’ve been trying to make a “Robot” move around an already made map. I’ve been trying to make lines of a text file into multiple arrays (ie. moving forward, turning left, turning right). Then, I’ve been trying to use values in those arrays to make the robot move. Basically, if the text file says:

1 0 0
0 1 0
0 0 1

So the first line is for moving, the second is for turning left and the third is for turning left. This is supposed to make the robot move, and then not turn. Then it moves on to the next column and doesn’t move, turns left, and doesn’t turn right. I’m trying to make sure it works no matter how long the code is. I’m able to put it into three arrays correctly, but I’m having trouble with this for loop. When the loop executes and the values are all 1’s or above, it works perfectly. However, if any value is a 0, it completely exits the loop and the robot doesn’t move. Any help or advice would be appreciated! Thank you!

Code is below:

public static void robotMove(Robot x) throws Exception {
  
     
     for (int i = 0; i < movedata.length; i++) {
     
        int y = movedata[i];
        int s = leftdata[i];
        int j = rightdata[i];
     
           for (int a = 0; a < movedata.length; a++) {
              
              if (y == 0) {
              //asking what to put here
              }
              else { 
                 again.Move(x, y);
              }
              
              if (s == 0) {
              //asking what to put here
              }
              else { 
                 again.Left(x, s);
              }
              
              if (j == 0) {
              //asking what to put here
              }
              else { 
                 again.Right(x, j);
              }
              
              
              
           }
     
     
     } 

Answer

Assuming that movedata, leftdata and rightdata are the 3 arrays of same size corresponding to each line of your example, without knowing what again is but assuming it is the object handling the .Move(), .Left() and Right() actions made on the Robot x, I think the following should be enough:

public static void robotMove(Robot x) throws Exception {
    for (int i = 0; i < movedata.length; i++) {
        if (y != 0) {
            again.Move(x, movedata[i]);
        }
        
        if (s != 0) {
            again.Left(x, leftdata[i]);
        }

        if (j != 0) {
            again.Right(x, rightdata[i]);
        }
    }
}
  • You can loop just once through movedata, I’ve removed the second nested loop.
  • If the value is 0, you do not want to do anything. Hence you don’t need an else part for that, just put code in the if value != 0.

From a code perspesctive, it would be cleaner if instead of having some if taking care that the value is not 0, you may implement the “inaction” directly in the methods .Move(), .Left() and .Right().



Source: stackoverflow