-2

I am trying to create a grid String matrix using a 2D array. The matrix should be mxm, this will vary on the user's input. The first column must consist of . and the columns thereafter must consist of *. I have imported the java.util.Arrays method and am using the toString function. The problem is that I am getting an output that has delimiters as well as prefixes and suffixes. The output looks like this:

[., *, *, *, *, *, *, *]
[., *, *, *, *, *, *, *]
[., *, *, *, *, *, *, *]
[., *, *, *, *, *, *, *]
[., *, *, *, *, *, *, *]
[., *, *, *, *, *, *, *]
[., *, *, *, *, *, *, *]
[., *, *, *, *, *, *, *]

However, I want the code to look like this:

.*******
.*******
.*******
.*******
.*******
.*******
.*******
.*******

So essentially I want to know if there is a way to get the matrix to have no square brackets or commas and how would I go about achieving this?

import java.util.Arrays;

public class trial1 {
 
 public static void main(String[] args) {
      StdOut.println("What size matrix would you like?");
      int m = StdIn.readInt();
     
     // create board
     String[][] gameBoard = new String[m][m];
     
     for(int i = 0; i < gameBoard.length; i++) {
         Arrays.fill(gameBoard[i], "*");
         gameBoard[i][0] = ".";
         StdOut.println(Arrays.toString(gameBoard[i]));
      }
        }

 }
  • Don't use `Arrays.toString`, but rather print out value by value directly. – RealSkeptic Apr 13 '22 at 21:04
  • So do you mean that I must manually change each element to '.' or is there a way of changing multiple elements at once. I am just asking because the array will change size, so this would make it difficult to hardcode. –  Apr 13 '22 at 21:09
  • 2
    Change `StdOut.println(Arrays.toString(gameBoard[i]));` to `StdOut.println(String.join("", gameBoard[i]));` – Eritrean Apr 13 '22 at 21:10

3 Answers3

1

One change seem to give you the results you want:

You can use String.join

Returns a new String composed of copies of the CharSequence elements joined together with a copy of the specified delimiter

public static void main(String[] args) {
      int m = 5;
     
     // create board
     String[][] gameBoard = new String[m][m];
     
     for(int i = 0; i < gameBoard.length; i++) {
         Arrays.fill(gameBoard[i], "*");
         gameBoard[i][0] = ".";
         System.out.println(String.join("", gameBoard[i]));
      }
    }
0

as you see, the method of Arrays.toString() use "[" as prefix, "," as delimeter and "]" as suffix. the easiest way to archieve what u want is to provide a custom toString method for your case:

public static void main(String[] args) {
    System.out.println("What size matrix would you like?");
    int m = 7; // just assume 7

    // create board
    String[][] gameBoard = new String[m][m];

    for (int i = 0; i < gameBoard.length; i++) {
        Arrays.fill(gameBoard[i], "*");
        gameBoard[i][0] = ".";
        System.out.println(toString(gameBoard[i]));
    }
}

private static String toString(String[] strings) {
    StringBuffer result = new StringBuffer();
    for (String string : strings) {
        result.append(string);
    }
    return result.toString();
}
  • A nice solution. Though better use `StringBuilder` which replaces `StringBuffer` as that is slower because of being synchronized, which is generally not needed. – Joop Eggen Apr 13 '22 at 22:48
0

The simple solution is just to loop through all the elements one by one, and print them out.

So, for your loop, you would do:

for (int i = 0; i < gameBoard.length; i++) {
     Arrays.fill(gameBoard[i], "*");
     gameBoard[i][0] = ".";

     //edited part
     for (int j = 0; j < gameBoard[0].length; j++){
          System.out.print(gameBoard[i][j]);
     }

     System.out.println();
}

And note: this is similar to icodetea's solution, but a little bit easier to understand.

axu08
  • 29
  • 1
  • 7