0

I've figured out how to create an array in which the output look like this:

1 2 3 4

5 6 7 8

9 10 11 12

13 14 15 16

if the user enters a 4 indicating a 4x4 array. My question is how could I manipulate this code in a way that it would out put the array in this order

1 2 3 4

8 7 6 5

9 10 11 12

16 15 14 13

where every other row is "backwards"

`import java.util.Scanner; import java.util.Arrays; import java.util.Arrays;

public class Question2 {

public static void main(String[] args) {

//Title
    System.out.println("[----------------------]");
    System.out.println("[     Array Pattern    ]");
    System.out.println("[----------------------]");
    System.out.println("");
//declare scanner
    Scanner keyboard = new Scanner (System.in);

//Prompt user to enter a digit greater than or equal to 3
    System.out.println("How many rows/columns do you want your array to have? (Must be at least 3):");

//read user input
    int num = keyboard.nextInt();

//place constraints on int num so that if it is less than 3, the program does not execute
    while(num<3 )
    {
        System.out.println("Lets's try this again....");
        System.out.println("How many rows/colums do you want your array to have? (Must be at least 3):");
        num = keyboard.nextInt();   
    }

    //2D array with number of rows and columns entered by user
    int[][] array = new int [num][num];
    int inc=1;
    for(int i=0;i<array.length;i++)
        for(int j=0;j<array.length;j++)
              {
                 array[i][j]=inc;
                 inc++;
              }


    //replace all square brackets in array display & format into order 

            //replace all commas in array display


    String a = Arrays.toString(array);
    a = Arrays.deepToString(array).replace("], [", "\n").replaceAll("[\\[\\],]", "");
    System.out.println(a);`
1
  • don't use Arrays.deepToString. looping manually would be easier, and more obvious. Commented Mar 30, 2017 at 18:13

2 Answers 2

1

This loop will do it:

int reverse = 0;   

for(int i=0;i<array.length;i++)
for(int j=0;j<array.length;j++)
      {
          if(i%2 == 0){
            if(j == 0){
                inc = reverse;
                if(i > 0 )inc = inc + num + 1;
            }
            array[i][j]=inc;
            inc++;  
          }
          else{
              if(j == 0)reverse = inc + num - 1;
              array[i][j]=reverse;
              reverse--;
          }
      }

Every other row it place numbers in a descending order.

Also to print out the numbers with tabs between them use:

String a = Arrays.toString(array);
a = Arrays.deepToString(array).replace("], [", "\n").replace(", ", "\t").replaceAll("[\\[\\],]", "");
System.out.println(a);

This will stop a table forming diagonally.

Sign up to request clarification or add additional context in comments.

8 Comments

thank you! What exactly is reverse and where did it come from?
@CheeseCracker no problem, I just quickly wrote it out :)
I tried running if but there seems to be a problem.. if i enter 3 then the ouput become 1 2 3 \n 3 2 1 \n 6 6 6
@CheeseCracker yeah just seen that sorry, ill fix it
@CheeseCracker Sorry for the logic error, it should be working now.
|
1

What you could do is reverse the order of the for loop on every other line, so that it decrements instead:

for(int i=0;i<array.length;i++)
{
    if(i%2 == 0){
        for(int j=0;j<array.length;j++)
        {
            array[i][j]=inc;
            inc++;
        }
    }
    else{
        for(int j=num-1;j>=0;j--)
        {
            array[i][j]=inc;
            inc++;
        }
    }
}

I don't really know how to format the columns the way you wrote the code (with Arrays.deepToString) but if you instead loop through it manually you could pad the string:

String [][]stringConvertedTable= new String[num][num];

    for(int i=0; i<num; i++) {
        for(int j=0; j<num; j++) {
            stringConvertedTable[i][j]= Integer.toString(array[i][j]);
            System.out.print(stringConvertedTable[i][j] + "\t");
        }
        System.out.println("");
    }

It's not the most elegant way to do it though...

2 Comments

Seems like someone beat me to it XD
also do you know how I could format the ouput to make the columns straight? If I enter a bigger number like 5+, then the output become kind of crooked?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.