touhidul islam kanon
touhidul islam kanon

Reputation: 33

Remove last comma in Two Dimensional Array using for loop

I can't avoid printing the last comma:

 public void displayArrayMethod(int array[][])
        {
            for(int row=0; row<array.length; row++)
            {
                for(int column=0; column<array[row].length; column++)
                {
                    System.out.print(array[row][column]+",");
                }
            }
        }

output: 1,2,3,4,5,6,7,8,9,

Upvotes: 1

Views: 404

Answers (4)

Chris Sharp
Chris Sharp

Reputation: 1995

This loop checks for the last column before appending the comma.

EDIT: Added full implementation to help find possible differences from user's that could be causing the problem in his comment. This code returns 1,2,3,4,5,6,7,8,9

public class Sandbox { //opens class 

    public static void main(String[] args) {
        int[][] array = {
                {1, 2, 3, 4, 5, 6, 7, 8, 9}
        };
        displayArrayMethod(array);
        System.out.println("wait");
    }

    public static void displayArrayMethod(int array[][]) {
        for(int row=0; row<array.length; row++)
        {
            if(row == array[row].length - 1 && column == array[row].length - 1)
            {
                if(column == array[row].length - 1) 
                {
                   System.out.print(array[row][column]);
                } 
                else 
                {
                   System.out.print(array[row][column]+",");
                }

            }
        }
    }
}

Upvotes: 1

NikNik
NikNik

Reputation: 2301

for(int column=0; column<array[row].length-1; column++){
   System.out.print(array[row][column]+",");
}
System.out.print(array[row][array[row].length-1];

Upvotes: 0

You can achieve this in several ways...

opt1: result.substring(0, result.length() - 1)

opt2: Arrays.deepToString(x) not too much what you need, but almost the same

opt3: stringBuilder (john skeet way...)

public static void main(final String[] args) {
    final int[][] x = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
    displayArrayMethod(x);
    System.out.println(Arrays.deepToString(x));
    displayArrayMethod2(x);
}

private static void displayArrayMethod2(final int[][] x) {
    final StringBuilder sb = new StringBuilder();
    String prefix = "";

    for (final int[] element : x) {
        for (final int element2 : element) {
            sb.append(prefix);
            prefix = ",";
            sb.append(element2);
        }
    }
    System.out.println(sb.toString());
}

public static void displayArrayMethod(final int array[][]) {
    String result = "";
    for (final int[] element : array) {
        for (final int element2 : element) {
            result += element2 + ",";
        }
    }
    System.out.println(result.substring(0, result.length() - 1));
}

Upvotes: 1

Mureinik
Mureinik

Reputation: 312344

Instead of looking for a way to get rid of the last comma, it would be much easier to avoid having it in the first place. One way to do so is to let Java's streams do the heavy lifting instead of you having to write loops yourself:

public void displayArrayMethod(int array[][]) {
    System.out.print(Arrays.stream(array)
              .flatMapToInt(Arrays::stream)
              .mapToObj(String::valueOf)
              .collect(Collectors.joining(",")));
}

Upvotes: 3

Related Questions