1

I'm new to java and am trying to output this array: -5, -2, -12, 7, 3, 15, 10

To look exactly like this: [ 0: -5, 1: -2, 2: 12, 3: 7, 4: 3, 5: 15, 6: 10 ]

public static void array(int[] numbers) {
        int index = 0;
        System.out.print("[");
        for (int i = 0; i < numbers.length; i++){
            System.out.print(index + ": " + numbers[i] + ", ");
            index ++;
        }
        System.out.print("]");
}

When I run my code it looks exactly like it should except has a comma after 10. Any ideas on how to get rid of the last comma or a better way to do this?

Thanks for any help! CK

cklw58
  • 11
  • 1
  • For the record, your `index` and `i` are always the same value here so you don't really need them both. If you like having the name `index` for readability, you can just use index in place of i. – Gary Jun 21 '15 at 23:37
  • possible duplicate of [Clearest way to comma-delimit a list (Java)?](http://stackoverflow.com/questions/668952/clearest-way-to-comma-delimit-a-list-java) – ChrisGuest Jun 21 '15 at 23:39
  • 1
    @ChrisGuest, I don't think this is a duplicate (of that question, at least) because this question is about arrays (not lists) and the OP wants to include the indices in the output. – Mick Mnemonic Jun 21 '15 at 23:42

6 Answers6

1

You don't need to add comma in print statement, when you read your last array index in the for loop. Here you go:

   for (int i = 0; i < numbers.length; i++){
        if(i == numbers.length -1) {
           System.out.print(index + ": " + numbers[i]);
        } else {
           System.out.print(index + ": " + numbers[i] + ", ");
        }
        index ++;
    }
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
1

Since you need the comma every time but the last time, you can do something like this:

System.out.print(index + ": " + numbers[i] + (i != numbers.length-1 ? ", " : "");

This will print out a comma except when on the last pass.

Also since i goes from zero to the limit, you don't need the index variable. You could just use i in place of `index'.

Always Learning
  • 5,510
  • 2
  • 17
  • 34
1

You could put an if statement that only prints the comma if it's not the last index.

for (int i = 0; i < numbers.length; i++){
    System.out.print(i + ": " + numbers[i]);
    if(i != numbers.length -1) {
       System.out.print(", ");
    }
}

Alternatively, print the first element before the loop and print each subsequent element with a comma before it.

System.out.println(0 + ": " + numbers[0]);

for (int i = 1; i < numbers.length; i++){
  System.out.print(", " + index + ": " + numbers[i]);
}
Gary
  • 13,303
  • 18
  • 49
  • 71
1

I would implement the method like this (note that index in your implementation is redundant because it's always equal to i):

public static void printArray(int[] numbers) {
    StringBuilder sb = new StringBuilder();

    for (int i = 0; i < numbers.length; i++) {
        if (sb.length() > 0) {
            sb.append(", ");
        }
        sb.append(String.format("%d: %d", i, numbers[i]));
    }
    System.out.print("[" + sb.toString() + "]");
}

I'm using StringBuilder which makes it easy (and effective) to compose a single String from the array contents. String.format is useful when you want to create formatted output.

Mick Mnemonic
  • 7,808
  • 2
  • 26
  • 30
1
System.out.print("[");
for (int i = 0; i < numbers.length; i++){
    if(numbers.length -1==i)
       System.out.print(i+ ": " + numbers[i]);
     else 
       System.out.print(i + ": " + numbers[i] + ", ");          
}
System.out.print("]");
D. Ben Knoble
  • 4,273
  • 1
  • 20
  • 38
1

With some minor changes to your code:

public static void array(int[] numbers) {
        //Check if the array isn't empty, you know, just in case ;)
        if (numbers.length == 0) {
            return
        }
        System.out.print("[ 0");
        for (int i = 0; i < numbers.length - 1; i++){
            System.out.print(": " + numbers[i] + ", " + (i+1));
        }
        System.out.print(": " + numbers[numbers.length-1] + " ]");
}

Resulting in [ 0: -5, 1: -2, 2: 12, 3: 7, 4: 3, 5: 15, 6: 10 ]

Roel Strolenberg
  • 2,922
  • 1
  • 15
  • 29