0

Currently I have the following method:

public static void printDoubleIntArray(int[][] doubleIntArray) {
for (int x = 0; x < doubleIntArray.length; x++) {
    System.out.println();
    for (int y = 0; y < doubleIntArray[x].length; y++) {
    System.out.print(doubleIntArray[x][y]);
    }
}
}

It works perfectly when the parameter "doubleIntArray" is only numbers from 0 - 9 like in the following print out:

0000000
0000300
0033332
0023323
0022223
0023233
0003332

However, if the integers in each element of the array become larger than 9 then I get something like the following:

0000000
000121797
001717171716
0101617171617
001616161617
081617161717
001417171716

What I would like to know is how do I make the above example print out like so:

0   0   0   0   0   0   0
0   0   0  12  17   9   7
0   0  17  17  17  17  16
0  10  16  17  17  16  17
0   0  16  16  16  16  17
0   8  16  17  16  17  17
0   0  14  17  17  17  16
2

6 Answers 6

2

You could try using java.text.NumberFormat and a pattern that displays every number with a fixed width.. Then concatenate them all in a single line...

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

Comments

2
System.out.print(doubleIntArray[x][y] + "\t");

\t prints a tab

but in this case it will print something like this: (but i guess thats okay for you)

0   0   0   0   0   0   0
0   0   0   12  17  9   7
0   0   17  17  17  17  16
0   10  16  17  17  16  17
0   0   16  16  16  16  17
0   8   16  17  16  17  17
0   0   14  17  17  17  16

1 Comment

It will heavily depend how OP wants to format the numbers for their consumption. For instance, since it looks for basic display purposes, this is a possible solution. Note that if you want a decent presentation format, it would be better using String#format instead.
2

Or use String.format("%4d", myinteger) to have each integer occupy 4 chars, and be properly right padded.

Comments

1
System.out.printf("%4d", doubleIntArray[x][y]);

4 represents the minimum space that the number will print.

The other options for this method are explained here

Comments

1

You have 2 options. Firstly, you can use \t in the second for loop. But I think you could add \t and whitespace character to avoid deterioration. Can provide this too, adding if-else structure in second for loop. I mean

if(doubleIntArray[y].length<10){
    System.out.print(doubleIntArray[x][y] + "\t  ");
    //Tab+two whitespace.
} else {
    if(doubleIntArray[y].length>10) {
        System.out.print(doubleIntArray[x][y] + "\t ");
        //Tab+one whitespace
    } else {
        System.out.print(doubleIntArray[x][y] + "\t");
        //Tab+NO whitespace
    }
}

Logic is that I think. Sorry for my answers design. I am on the bus now and I cannot write smoothly. If I had a mistake sorry about that again.

Comments

0
public static void printDoubleIntArray(int[][] doubleIntArray) {
for (int x = 0; x < doubleIntArray.length; x++) {
System.out.println();
 for (int y = 0; y < doubleIntArray[x].length; y++) {
System.out.print(doubleIntArray[x][y],"\t");
    }
 System.out.print("\n");
}
}

Comments

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.