Olaf Schmidt
Olaf Schmidt

Reputation: 3

Printing an Array into String

For an Assignment I have to create a Five in a Row Game with a print Method. So basically my Idea is that the Method goes through the whole two dimensional Array and everytime there is a 0 it adds a "-" to the String row instead. When there is 1 or 2 it adds the Number. When the [j] Array is finished it prints the whole row and initializes the row anew. Terminal is another Class which was given to us to print everything in the Class.

The Problem is the whole Method doesn't print anything at all and I don't know where the Problem could be.

public static void print() {
    String row = "";
    for (int i = 0; i < board.length; i++) {
        for (int j = 0; j < board[i].length; j++) {
            if (board[i][j] == 0) {
                row += "-";
            } else  {
                row += (char) (board[i][j]);
            }
            if (j == board[i].length) {
               Terminal.printLine(row);
               row = "";
            }
        }
    }
}

Upvotes: 0

Views: 124

Answers (4)

Valentin Michalak
Valentin Michalak

Reputation: 2147

Other peoples in this question attempt to help you with your existent code. Take some time to discover Java 8 Streams API, your problems is pretty simple to solve with it !

String[][] board = {{"a", "a", "a"}, {"b", "c", "d"}, {"e", "f", "g"}};

Arrays.stream(board).map(r -> String.join("-", r)).forEach(System.out::println);

Step by step:

  1. Arrays.stream(board) transform your board array to a stream of String[].

  2. .map(r -> String.join("-", r)) map all your String[] to String where each elements are joined by "-"

  3. .forEach(System.out::println) wrote it directly on the console (you could use Terminal::printLine in your case if you prefer here).

Upvotes: 1

Can Bayar
Can Bayar

Reputation: 541

Try replacing if (j == board[i].length) block with if (j == board[i].length - 1). Since your for loops continues until board[i].length, it will never enter your if statement.

Additionally, if your array is an integer array and cast an integer to char using (char) (board[i][j]), you will get the ASCII value with value 1, which will not be equal to '1', '2', etc. If that's the case, use (char)(board[i][j] + '0').

Upvotes: 0

Zaki Petrov
Zaki Petrov

Reputation: 86

Firstly i suggest you to use StringBuilder append method instead of String concatenation. The program will never enter in this if statement, because j will never be equal to board[i].length.

if (j == board[i].length) {
    Terminal.printLine(row);
    row = "";
}

Instead you can compare j to board[i].length - 1 but this is just useless. You can move the Terminal.printLine(row) outside of the inner for loop.

I will refactor the code for you:

public static void print() {
    StringBuilder row = new StringBuilder();
    for (int i = 0; i < board.length; i++) {
        for (int j = 0; j < board[i].length; j++) {
            if (board[i][j] == 0) {
                row.append("-");
            } else  {
                row.append((char) (board[i][j]));
            }
        }
        Terminal.printLine(row.toString());
        row = new StringBuilder();
    }
}

If the StringBuilder is inefficient in this case you can still use String but my opinion is that StringBuilder is always better.

Upvotes: 0

Smankusors
Smankusors

Reputation: 387

Because variable j will never be equal as length of board[i].

Your for loop i is only iterate from 0, 1, 2, ..., board.length - 1.

Same with your loop j. It is only iterate from 0, 1, 2, ..., board[i].length - 1

Except if you are using j <= board[i].length, but it probably will throw IndexOutOfRangeException.

Your code should be like this.

public static void print() {
    String row = "";
    for (int i = 0; i < board.length; i++) {
        for (int j = 0; j < board[i].length; j++) {
            if (board[i][j] == 0) {
                row += "-";
            } else  {
                row += (char) (board[i][j]);
            }
            if (j == board[i].length - 1) {
               Terminal.printLine(row);
               row = "";
            }
        }
    }
}

Upvotes: 1

Related Questions