5

I'd like to print an inputed 2-dimensional array like a table i.e if for some reason they put in all 1s...

1 1 1 1 

1 1 1 1 

1 1 1 1

1 1 1 1 

Just like so above but in the console on Java eclipse, no fancy buttons and GUI's but in the console, here is what I have....

    import java.util.Scanner;
    public class Client {
        public static void main(String[] args){
            Scanner input = new Scanner(System.in);

            int[][] table = new int[4][4];
            for (int i=0; i < table.length; i++) {
                for (int j=0; j < table.length; j++) {
                    System.out.println("Enter a number.");
                    int x = input.nextInt();
                    table[i][j] = x;
                    System.out.print(table[i][j] + " "); 
                } 
                    System.out.println();
        }
        System.out.println(table);
    }
}

And this is what I get when I input everything and the console terminates:

Enter a number.

1

1 Enter a number.

1

1 Enter a number.

1

1 Enter a number.

1

1 

[[I@3fa1732d
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Adam Calcanes
  • 89
  • 1
  • 3
  • 8
  • I down-vote for a reason such as: 'lack of research'. – e.doroskevic Jan 06 '14 at 22:01
  • Sorry but I couldn't find this anywhere if you could show me a question that asks how to print an array like so in the console then I won't question your downvote – Adam Calcanes Jan 06 '14 at 22:16
  • Well then, thanks, It was probably the hyphen in the two-dimensional part of the title that threw off my search as I was also using 2d instead of two dimensional because it gave me more results – Adam Calcanes Jan 06 '14 at 22:52

5 Answers5

8

Consider using java.util.Arrays.

There is a method in there called deepToString. This will work great here.

System.out.println(Arrays.deepToString(table));

Relevant here: Simplest way to print an array in Java

Community
  • 1
  • 1
Obicere
  • 2,999
  • 3
  • 20
  • 31
  • Thanks for the help, I would upvote your answer if I had the rep but apparently my question deserved 2 downvotes, this is very useful, I'm just not too aware with the array utility so I went for the more complicated but what I better understand, and just modified my FOR loop like Tonga said I will definitely look into this and try to learn more about your answer. – Adam Calcanes Jan 06 '14 at 22:19
  • @AdamCalcanes sounds great. The post I linked contains a lot of information on how to accomplish this. Always great to learn new ways :D – Obicere Jan 06 '14 at 22:20
1

You need to print out the array separately from entering the number. So you can do something like this:

public class PrintArray {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        int[][] table = new int[4][4];
        for (int i = 0; i < table.length; i++) {
            for (int j = 0; j < table.length; j++) {
                // System.out.println("Enter a number.");
                int x = input.nextInt();
                table[i][j] = x;
            }
            //System.out.println();
        }
        // System.out.println(table);

        for (int i = 0; i < table.length; i++) {
            for (int j = 0; j < table[i].length; j++) {
                System.out.print(table[i][j] + " ");
            }
            System.out.println();
        }
    }
}
tonga
  • 11,749
  • 25
  • 75
  • 96
0

You'll have loop back through those arrays to print out the contents. an array's toString() just prints the reference value.

evanchooly
  • 6,102
  • 1
  • 16
  • 23
0

System.out.print(table) calls a method in array class that prints out the identifier of the vairable. you need to either create a for loop that will print out each element like System.out.print(table[i][j]) or use the Arrays class and say Arrays.toString(table);

Louis B
  • 342
  • 1
  • 5
  • 21
0

Try copying this simple for loop to printing a 4x4 table:

 Scanner input = new Scanner();
    int numArray [] [] = new int [4] [4];
    for ( int c = 0; c < 4; c++ ) {
    for (int d = 0; d < 4; d++) { 
    System.out.print("Enter number : ");
    nunArray [c][d] = input.nextInt();
    } 
    }
    for (int a = 1; a<5;a++) {
    for (int b = 1; b <5;b++) {
    System.out.print(numArray [a][b]+" ");
    }
    System.out.println();
    }
Adam Calcanes
  • 89
  • 1
  • 3
  • 8
111
  • 1
  • 3
  • @Adam Calcanes there are errors in your code : line 6, variable should be 'numArray' instead of 'nunArray'. line 11, will also generate java.lang.ArrayIndexOutOfBoundsException. – Kenny Dabiri Oct 26 '16 at 12:48