-1

I was trying to remove the last comma in the loop so it wouldn't show the extra comma on the last part. Already tried experimenting bunch of times on the code but it does not work at all.

System.out.println("--Unsorted Array--");
  for(int i = 0; i < mergeS.length; i++){
    System.out.print(mergeS[i]);
    for(int j = 0; j < 1; j+=mergeS[i]-1){
      System.out.print(", ");
  }
}

My expected output is 95, 85, 75, 65, 55 but the actual output is 95, 85, 75, 65, 55,

Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
  • 2
    You could check the value of `i`. Also note, what you're trying to do here is typically done using the `join` method; unless you need to do it from scratch. – Carcigenicate Oct 08 '19 at 12:18
  • You have two obvious options: (1) you put a comma in every iteration and remove the last one after the loop or (2) you check the current index in every iteration and only put a comma if the current element is not the last one. – deHaar Oct 08 '19 at 12:19
  • maybe https://stackoverflow.com/a/58259100/85421 or just add the "," **before** each value is printed if `i > 0`; also check the `StringJoiner` class – user85421 Oct 08 '19 at 12:22
  • @Nexevis That's a good point, kinda forgot they were ints! – JonK Oct 08 '19 at 12:34

8 Answers8

2
System.out.println("--Unsorted Array--");
for (int i = 0; i < mergeS.length; i++) {
    if (i == 0) {
        System.out.print(mergeS[i]);
    } else {
         System.out.print(", " + mergeS[i]);
    }
}
deHaar
  • 17,687
  • 10
  • 38
  • 51
TheCoder
  • 128
  • 12
1

try using streams

System.out.println(Arrays.stream(mergeS).mapToObj(String::valueOf).collect(Collectors.joining(",")));
heyhooo
  • 82
  • 6
0

You could conditionally print the comma using an if statement, if you are on the last iteration of your outer for loop, you can not print it, for all other iterations before the last, you can print it:

if(i < mergeS.length-1) // only print ',' when i is not on the last iteration

Also, note, there is no need for your inner for loop, as all you need to do is loop over your items in your array, and so, no inner loop is needed

See example below:

System.out.println("--Unsorted Array--");
for(int i = 0; i < mergeS.length; i++){
  System.out.print(mergeS[i]);
  if(i < mergeS.length-1)
    System.out.print(", ");    
}
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
0

A small adjustment to your code does what you want it to do:

public static void main(String[] args) {
    int[] mergeS = { 95, 85, 75, 65, 55 };
    System.out.println("--Unsorted Array--");
    for (int i = 0; i < mergeS.length; i++) {
        System.out.print(mergeS[i]);
        // check if the current element is the last one
        if (i < mergeS.length - 1) {
            // and print a comma if it isn't
            System.out.print(", ");
        }
    }
}
deHaar
  • 17,687
  • 10
  • 38
  • 51
0

If you want to avoid an if check.

String delimiter = "";
for (int i = 0; i < mergeS.length; i++) {
    System.out.print(delimiter);
    System.out.print(mergeS[i]);
    delimiter = ", ";
}
Ashwani
  • 1,938
  • 11
  • 15
0

You could change your for loop to an iterator

Set<String> names = new HashSet<>();
Iterator<String> iterator = mergeS.iterator();
while (iterator.hasNext()) {
    System.out.print(iterator.next());
    if (iterator.hasNext()) {
        System.out.print(",");
    }
}

But I prefer the using of a stream

System.out.println(Arrays.stream(mergeS).mapToObj(String::valueOf).collect(Collectors.joining(",")));
Dilyano Senders
  • 199
  • 1
  • 9
0

Do it this way:

System.out.println("--Unsorted Array--");
  for(int i = 0; i < mergeS.length; i++){
    System.out.print(mergeS[i]);
    if(i < mergeS.length-1){
      System.out.print(", ");
  }
}
Hamlet
  • 307
  • 1
  • 8
0

Other people already showed how to prevent the final comma from printing efficiently, but you also have the option of using Arrays.toString(), if you do not mind having brackets as the output and just want to view the array:

int[] mergeS = { 10, 20, 30, 40, 85 };
System.out.println("--Unsorted Array--");
System.out.println(Arrays.toString(mergeS));

Output:

--Unsorted Array--

[10, 20, 30, 40, 85]

Of course you can easily remove the brackets with a call to substring as well:

int[] mergeS = {10, 5, 12, 31};
System.out.println("--Unsorted Array--");
String str = Arrays.toString(mergeS);
System.out.println(str.substring(1, str.length() - 1));

Output:

--Unsorted Array--

10, 5, 12, 31

Nexevis
  • 4,647
  • 3
  • 13
  • 22