0

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 = "";
            }
        }
    }
}
5
  • What is Terminal? Also note that since j < board[i].length, it will never be equal to it, move your print to after the inside for loop Commented Jan 30, 2018 at 15:09
  • 1
    if(j == board[i].length) should be if(j == board[i].length - 1). Or, as phflack said, move it after the inner for loop. Commented Jan 30, 2018 at 15:10
  • @phflack Terminal is a Class provided the University to print or read things in the Class. We need to use it to print it (Not allowed to use System.out.print etc) Commented Jan 30, 2018 at 15:11
  • @phflack Ok thx that did do the trick. Didn't think that that was the Problem Commented Jan 30, 2018 at 15:14
  • This question is similar to: What's the simplest way to print a Java array?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Aug 19 at 11:15

4 Answers 4

1

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 = "";
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

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).

Comments

0

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.

Comments

0

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').

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.