0

I want to output table that shows row and column indexes [i][j] for every cell ,

I can only print i-index but don't know how to print "column" index

It should look like this: table for every column - there is indexes shown above - first horizontal line - 0 1 2 3 4 5 6 7 8 9 10

The code i have :

public static void print_table( int table[][] ){
    System.out.println( "PRINTING TABLE ") ;

    for(int i = 0; i <= NCHANGES; i++) {
        System.out.print("[" + i + "]") ;// this prints i-index for EVERY ROW
        for(int j = 0; j <= MAX_AMOUNT; j++) {
            System.out.print(table[i][j] + "\t") ;
            // BUT HOW TO PRINT J(COLUMN) INDEX for every column?
        }
        System.out.println() ;
    }
}

I'm sure this will be useful for all who need to print out the result of their 2-nested loops

4 Answers 4

2

Before doing the nested loops just make a for loop and print
- the I/J cell
- the column header cells (0, 1, 2, etc.)
- new line

Then do what you're already doing.

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

Comments

1

Something like this perhaps?

    public static void main(String[] args) {
    int[][] twoD = new int[][] { { 123, 456, 789 }, { 123, 456, 789 },
            { 123, 456, 789 } };

    StringBuilder sb = new StringBuilder();

    for (int I = 0; I < twoD.length; I++) {
        for (int X = 0; X < twoD[I].length; X++) {
            sb.append("Row " + I);
            sb.append(" Column " + X);
            sb.append(" Value " + twoD[I][X]);
            sb.append("\n");
            System.out.print(sb.toString());
            sb.setLength(0);
        }
    }
}

Console output:

Row 0 Column 0 Value 123
Row 0 Column 1 Value 456
Row 0 Column 2 Value 789
Row 1 Column 0 Value 123
Row 1 Column 1 Value 456
Row 1 Column 2 Value 789
Row 2 Column 0 Value 123
Row 2 Column 1 Value 456
Row 2 Column 2 Value 789

2 Comments

mmm not that exactly - but thanks for looking at my question!
lookup my answer - this is how i wanted it to be - i get it done!
0

Maybe this?

public static void print_table( int table[][] ){
 System.out.println( "PRINTING TABLE ") ;

 //Extra loop here
 System.out.print("I/J ");
 for (int j = 0; j <= MAX_AMOUNT; j++){
     System.out.print(j + " ");  
 }

 for(int i = 0 ; i <= NCHANGES ;i++){

   System.out.print("[" + i + "]") ;// this prints i-index for EVERY ROW

         for(int j = 0 ; j <= MAX_AMOUNT  ; j++){

      System.out.print(table[i][j] + "\t") ;// BUT HOW TO PRINT J(COLUMN) INDEX for every column?

                 }
                 System.out.println() ;
         }

 }

Comments

0
public static void print_table( int table[][] ){
 System.out.println( "\n\nPRINTING TABLE ") ;

 System.out.println("\t\tm") ;
  System.out.print("\n        ") ;
 for(int column = 0 ; column <= MAX_AMOUNT; column++){
           System.out.print(  column  + " \t"  ) ;
 }
 System.out.print("\n----------------------------------------------------------------------------------------------------------------------------------") ;
 System.out.println("----------------------------------------------------------") ;
 for(int i = 0 ; i <= NCHANGES ;i++){
   System.out.print("[" + i + "]:  ") ;
         for(int j = 0 ; j <= MAX_AMOUNT  ; j++){

                    System.out.print(table[i][j] + "\t") ;

                 }
                 System.out.println() ;
         }

 }

EXACTLY AS I WANTED IT! table

1 Comment

If one of the answers here helped you solve it, accept the answer please.

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.