Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

For a program I have to write I am required to turn a 2 dimensional array of single digit intgers into a string of numbers separated by spaces. A test code is provided for me and when I run it, my code fails both arrayToString tests. I am unable to find where my logic error is. Someone help!

public static String arrayToString(int[][] a) {

    String aString;     
    aString = "";
    int column;
    int row;

    for (row = 0; row < a.length; row++) {
        for (column = 0; column < a[0].length; column++ ) {
        aString = aString + " " + a[row][column];
        }
    aString = aString + "\n";
    }

    return aString;
}
share|improve this question
    
Maybe it doesn't like the extra space at the beginning of every line. Is it possible for you to post the test code, so we can see? –  David Wallace Nov 17 '13 at 20:23
    
Since your code works as expected, you have to tell us what the expected output (aka: tests) look like. –  Jeroen Vannevel Nov 17 '13 at 20:25
    
It could possibly be the extra space at the beginning, however I do not see the space in the output when I do it. Where could I post the test code for you guys to see it? @DavidWallace –  Alex A Nov 17 '13 at 21:04
    
Edit the question. Append the test code to it. –  David Wallace Nov 17 '13 at 21:20
add comment

3 Answers

Your code is basically OK, except you are putting a leading space at the start of every line. To clean them up before returning, add this line:

aString = aString.replaceAll("(?m)^ ", "");

This uses regex to delete (by replacing with a blank) all spaces at the start of lines. The regex flag (?m) makes ^ match the start of every line of the input (normally ^ matches only the start of input).

share|improve this answer
add comment

I can think of three possible issues.

  1. The test code doesn't want to see the space that you are putting at the beginning of every line.
  2. The test code is passing an array where some of the entries are null. Your code doesn't cope with this.
  3. The test code is passing an array where the entries have different lengths. Your code doesn't cope with this either.

Until you post the test code, we can't see which one the problem is. But you could deal with all three by changing your loop to this.

for (row = 0; row < a.length; row++) {
    if (a[row] != null && a[row].length > 0) {
        aString = aString + a[row][0];

        for (column = 1; column < a[row].length; column++) {
            aString = aString + " " + a[row][column];
        }
    }
    aString = aString + "\n";
}
share|improve this answer
add comment
int[][] a = new int[][]{{1,2,3},{4,5,6},{7,8,9}};

public static String arrayToString(int[][] a) {

    String aString;     
    aString = "";
    int column;
    int row;

    for (row = 0; row < a.length; row++) {
        for (column = 0; column < a[0].length; column++ ) {
        aString = aString + " " + a[row][column];
        }
    aString = aString + "\n";
    }

    return aString;
}

Output:

1 2 3
4 5 6
7 8 9

If you skip this line aString = aString + "\n"; your output will look like this

1 2 3 4 5 6 7 8 9
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.