0

Basically I am trying to create an object and calling it via the main method/ a constructor. When I print it out I get a list but I needed it printed out like a table. Here is the code:

import java.util.Arrays;

public class test2 {
    public static String[][] makeTable(int mouseLocationRow, int mouseLocationColumn) {
        int rows = 12;
        int columns = 12;
        String[][] a = new String[rows][columns];
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                a[i][j] =   "#";
            }
        }
        a[mouseLocationRow][mouseLocationColumn] = "&";
        return a;
    }
    public static void main(String[] args) {
        int a = 5;
        int b = 5;
        System.out.println(Arrays.deepToString(makeTable(a, b)));
    }

}

Here is the output:

[[#, #, #, #, #, #, #, #, #, #, #, #], [#, #, #, #, #, #, #, #, #, #, #, #], [#, #, #, #, #, #, #, #, #, #, #, #], [#, #, #, #, #, #, #, #, #, #, #, #], [#, #, #, #, #, #, #, #, #, #, #, #], [#, #, #, #, #, &, #, #, #, #, #, #], [#, #, #, #, #, #, #, #, #, #, #, #], [#, #, #, #, #, #, #, #, #, #, #, #], [#, #, #, #, #, #, #, #, #, #, #, #], [#, #, #, #, #, #, #, #, #, #, #, #], [#, #, #, #, #, #, #, #, #, #, #, #], [#, #, #, #, #, #, #, #, #, #, #, #]]

Here is how its supposed to look like:

############
############
############
############
############
#####&######
############
############
############
############
############
############

How do I make my output look like how its supposed to look like?

2 Answers 2

1

You might want to use char[][] instead of String[][]..

Then, to display, use something like printTable() below:

import java.util.Arrays;

public class test2 {
    public static char[][] makeTable(int mouseLocationRow, int mouseLocationColumn) {
        int rows = 12;
        int columns = 12;
        char[][] table = new char[rows][columns];

        for (char[] row : table)
            Arrays.fill(row, '#');

        table[mouseLocationRow][mouseLocationColumn] = '&';
        return table;
    }

    public static void printTable(char[][] table) {
        for (char[] row : table)
            System.out.println(new String(row));
    }

    public static void main(String[] args) {
        int a = 5;
        int b = 5;
        printTable(makeTable(a, b));
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

the reason I used a string was because # is a string and won't let me use a char[][], it says type mismatch cant convert from a string to a char. How do I make it so that the char[][] takes # as an input?
you have to use '#' instead of "#" (single quotes for chars, double quotes for strings)
oh I fixed it maybe I used the ascii code for # and it worked. Testing out full code now.
Ok I wasn't able to get it working, where would I put that code?
0

Are you looking for something like :

 public void print2DArray(int[][] arr) {
    for (int row = 0; row < arr.length; row++) {
        for (int col = 0; col < arr[row].length; col++) {
            System.out.print(" " + arr[row][col]);
        }
        System.out.println();
    }
}

And Call it from your main method like this:

    int[][] arr = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
    print2DArray(arr);

Or did i miss something ?

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.