1
public static void main(String[] args) {
    char [][] charArr = 
    {   {'a','b','c'},
        {'d','e','f'},
        {'g','h','i'}
    };

    String [] stringA = charToString(charArr);

    for (int i = 0; i < stringA.length; i++) 
    {
        System.out.println(stringA[i]);
    }

}

public static String [] charToString(char [][] array)
{
    String [] stringArr = new String [array.length];
    for (int i = 0; i < array.length; i++) 
    {
        stringArr[i] += "";
        for (int j = 0; j < array[i].length; j++)
        {
            stringArr[i] += array[i][j];
        }
    }
    return stringArr;

Currently I am getting an output of:

nullabc

nulldef

nullghi

I am trying to concatenate each column of the 2d array charArr into a string and into each element of the 1d array called stringArr. I'm not sure where null is coming from though or what I'm doing wrong...

Appreciate the help!

3
  • 1
    One liner for that method return Arrays.stream(array).map(String::new).toArray(String[]::new) Commented Dec 4, 2018 at 6:20
  • Can you explain this to me? Commented Dec 4, 2018 at 20:38
  • 1
    Explained here in an answer Commented Dec 5, 2018 at 2:10

5 Answers 5

7
stringArr[i] += "";

this line is causing the issue, which is equivalent to stringArr[i] = stringArr[i]+ ""; you are declaring a null string and appending a empty string to it, simply change it to

stringArr[i] = "";
Sign up to request clarification or add additional context in comments.

Comments

1

When this line execute

String [] stringArr = new String [array.length];

It create an array and initialize array element with null. So when you perform concat(stringArr[i] += "") it add null then data.

You need to change your method like this-

public static String [] charToString(char [][] array)
    {
        String [] stringArr = new String [array.length];

        for (int i = 0; i < array.length; i++) 
        {
            if(stringArr[i]==null){
                stringArr[i]="";
            }
            stringArr[i] += "";
            for (int j = 0; j < array[i].length; j++)
            {
                stringArr[i] += array[i][j];
            }
        }
        return stringArr;
    } 

Comments

0

This is not perhaps an exact fix for your code, but can take advantage of the String constructor which accepts a character array as input.

char [][] charArr = {
    {'a','b','c'},
    {'d','e','f'},
    {'g','h','i'}
};

StringBuilder sb = new StringBuilder();
for (int i=0; i < charArr.length; ++i) {
    if (i > 0) sb.append("\n");
    sb.append(new String(charArr[i]));
}
System.out.println(sb);

abc
def
ghi

Comments

0
// < java 8
public static String[] charToString(char[][] arr) {
    String[] res = new String[arr.length];

    for (int i = 0; i < arr.length; i++)
        res[i] = new String(arr[i]);

    return res;
}

// >= java 8
public static String[] charToString(char[][] arr) {
    return Arrays.stream(arr).map(String::new).toArray(String[]::new);
}

Input:

{
    { 'a', 'b', 'c' },
    { 'd', 'e', 'f' },
    { 'g', 'h', 'i' }
}

Output:

abc
def
ghi

Notes:

  1. Bad practice to concatenate strings with loop; do use StringBuilder instead;
  2. It is better to use out-of-box method, like. new String(char value[]) instead of build it using StringBuilder.
  3. Streams is good, but sometimes it is too hard to read; do not hesitate to use POJO and loops in case it looks more readable.

Comments

0

Using streams one could update the method as:

private static String [] charToString(char[][] array) {
    return Arrays.stream(array) //forms a Stream<char[]> (all your rows)
            .map(String::new) // maps the char[] to String forming Stream<String>
            .toArray(String[]::new); // converts the stream to array String[]
}

and further use it as :

char[][] charArr =
            {{'a', 'b', 'c'},
                    {'d', 'e', 'f'},
                    {'g', 'h', 'i'}
            };

Arrays.stream(charToString(charArr)).forEach(System.out::println);

Output

abc
def
ghi

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.