0

can somebody help me with this code please? I have a main-Method and a private method wrap(). The aim is that the wrap() Method receive a String and have to output a 2-Dimensional Array. The big Problem is, that it does not work as it work with one Dimensional Arrays. Instead the program gives me this "[[C@7ad041f3% ". How can I make the program to give me 2-Dimensional array? I want to mention that I do not want to change my method by replacing "char[][]" with "void"

public class Probe {

    private static char[][] wrap(String message) {

        char[] chars = message.toCharArray();
        char[][] charArray = new char[4][7];
        int index = 0;

        for (int zeilenIndex = 0; zeilenIndex < charArray.length; zeilenIndex++) {
            for (int spaltenIndex = 0; spaltenIndex < charArray[zeilenIndex].length; spaltenIndex++) {
                charArray[zeilenIndex][spaltenIndex] =  chars[index];
                index++;
            }
        }
        return charArray;
    }

    public static void main(String[] args) {
        char[][] chars = new char[4][7];
        String text = "WIR GREIFEN MORGEN MITTAG AN";
        chars = wrap(text);
        System.out.print(chars);
    }
}

My question was closed. So I explain here why my question is different. As I already mentioned I am not allowed to change my method. I know that with little difference I could get the wanted results. For example, rewrite the method from "private static char[][] wrap(String message)" to "private static VOID wrap(String message)" and then just make additional loops to print the wanted results. But that is not the solution to the problem I want a solution. I want that my method gives the 2d Array back without changing the method, so how to do that?

  • 1
    Can you use [`Arrays.deepToString()`](https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/Arrays.html#deepToString(java.lang.Object%5B%5D))? – Ole V.V. Mar 01 '23 at 09:19
  • 1
    Outputting arrays does not work the naïve way. Printing a 1-dimensional int or string array will give you something similar to what you see. So your real question is how to print an array. There is an exception for 1-dimensional *char* arrays since there is an overridden `System.out.print(char[])` that perceives the char array as a string. Only because of this you didn’t discover any issue with printing your one-dimensional array. – Ole V.V. Mar 01 '23 at 09:22
  • 1
    Tip of the day: search before asking. – Ole V.V. Mar 01 '23 at 09:25
  • There is nor information, I do not know why my question was closed – Gert Wiebach Mar 01 '23 at 10:44
  • This question was previously closed as duplicate of the following three questions: [Trying to print a 2D array of Chars in Java](https://stackoverflow.com/questions/13475820/trying-to-print-a-2d-array-of-chars-in-java), [Iterate through 2 dimensional array](https://stackoverflow.com/questions/25798958/iterate-through-2-dimensional-array) and [What's the simplest way to print a Java array?](https://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array). The OP was unable to find the necessary information there, so I have reopened. – Ole V.V. Mar 01 '23 at 11:40
  • I didn’t get what you were still missing after consulting the answers under those earlier questions. They generally do not require you to change your method. – Ole V.V. Mar 01 '23 at 11:42
  • It’s unreadable in a comment, so paste into your IDE and reformat: `for (char[] line: chars) { System.out.println(line); } `. It prints 4 lines, each line 7 characters long. – Ole V.V. Mar 01 '23 at 11:46

0 Answers0