0

This has been driving me crazy all day. I've been trying to get the arrays to stop printing a comma after the last number that's printed for that array. I just can't seem to get it to work properly. Any help at all would be greatly appreciated.

This is the output of the arrays:

[5, 6, 7, 8, ]

[[2, 4, 6, 8, ] [8, 7, 9, 1, ] [35, 1, 2, ]]

[[12, ] [3, 4, 5, ] [6, ] [7, 8, 9, ]]

The output I want:

[5, 6, 7, 8]

[[2, 4, 6, 8] [8, 7, 9, 1] [3, 5, 1, 2]]

[[1, 2] [3, 4, 5] [6] [7, 8, 9]]

public class ArrayPrinter {

public static void main(String[] args) {
    int[] oneD = {5 ,6 ,7 ,8};
  
    int[][] twoD = {{2, 4, 6, 8}, {8, 7, 9, 1}, {3, 5, 1, 2}}; 
   
    int[][] twoD2 = {{1, 2}, {3, 4, 5}, {6}, {7, 8, 9}};
   
    printArray(oneD);
    printArray(twoD);
    printArray(twoD2);
    
}

public static void printArray(int[] arr) 
{
   int l = arr.length;
    System.out.print("[");
    for (int i = 0; i < l; i++) 
    {
        if (arr [i] == l-1)
            System.out.print(arr[i]);
        
       else
            System.out.print(arr[i] + ", ");
    }
    
    System.out.println("]");
    
}

public static void printArray(int[] [] arr) 
{
    
    int l = arr.length;
    System.out.println("[");
       for (int i=0; i < l; i++) 
        printArray(arr[i]);
    System.out.println("]");
   }
}
Community
  • 1
  • 1
  • 1
    You do the same thing whether `arr [i] == l-1` or not; you might want to start there. – Scott Hunter Nov 27 '16 at 00:22
  • I'm aware of this, the only problem is if I don't do that, some of the numbers in the array will print as [12, ] instead of [1, 2] – user7214683 Nov 27 '16 at 00:24
  • When you say "if I don't so that", what are you doing instead? As it is, the `if` is pointltess, because you do exactly the same thing whether the test is true or false. – Scott Hunter Nov 27 '16 at 00:26
  • Hmm actually I'll follow your suggestion and update/edit the post as soon as I can. I have a feeling you're right and that the problem with spaces and comma's not printing is related to the way I get the array length. – user7214683 Nov 27 '16 at 00:42
  • Related http://stackoverflow.com/questions/18279622/print-out-elements-from-an-array-with-a-comma-between-elements-except-the-last-w and http://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array – OneCricketeer Nov 27 '16 at 01:32
  • Tried @Scott Hunter's suggestion. Now some of the numbers are printing without a comma or space between them – user7214683 Nov 27 '16 at 02:16

2 Answers2

0

If you only want to print a comma after each element that is not the last, you want to check its position, not its value:

if ( i < l-1 )
   System.out.print(arr[i] + ", ");
else
   System.out.print(arr[i]);
Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
0

For a solution with no loop or check of indexes, you can use the Arrays.toString method that does exactly what you want for 1D array:

public static void printArray(int[] array) {
    System.out.println(Arrays.toString(array));
}

For 2D arrays then you can use java 8 streams combined with the previous method:

public static void printArray(int[][] array) {
    String arr2d = Arrays.stream(array)             // build a stream of int[]
        .map(x -> Arrays.toString(x))               // format each int[] into a pretty string
        .collect(Collectors.joining("", "[", "]")); // format the 2d array
    System.out.println(arr2d);
}
T.Gounelle
  • 5,953
  • 1
  • 22
  • 32