Search code examples
javaarraysloopsjavascriptserializer

airline seat using for


I want my result to appear like this, each three in a line

********Flight SA220*******

000

000

000

000

000

but it ends up like this

**** Flight SA220 ****

000000000000000

any suggetions on what to do ??

here is my java code

System.out.println("**** Flight "+flightCode[0]+" ****");   
  for(int j=0 ; j<seat[0].length ; j++)
    for (int k=0; k< seat[0][j].length;k++)         
        if (seat[0][j].length%3 == 0)
         System.out.println(seat[0][j][k]);
        else
         System.out.print(seat[0][j][k]);

Solution

  • In your if you need to change your seat[0][j].length to k. The length of the array never changes, you need to check if the index (where the loop is in the array) is %3

    System.out.println("**** Flight "+flightCode[0]+" ****");
    for(int j=0 ; j<seat[0].length ; j++){
        for(int k=0; k< seat[0][j].length;k++){     
            if(k%3 == 0){
                System.out.println(seat[0][j][k]);
            }
            else{
                System.out.print(seat[0][j][k]);
            }
        }
    }