Q:

Output values to be separated by commas and to exclude the last comma

I need my output to be separated by commas which it does, but I need the last comma excluded and also how would I rerun my program. I need to do this the simplest way as possible. This is what I have so far:
System.out.print("Enter numbers (-1 to end):");

  int num = input.nextInt();      
  int sum = 0;
  String u= " ";

  while (num != -1) {
    sum += num;
    u += num + ",";
    num =  input.nextInt();
  }
System.out.println("Entered Numbers: " + u);

System.out.println("The Sum: " + sum);

A:

Replace
u += num + ",";

with
u += (u.length() == 1 ? "" : ",") + num;

This only appends the comma if something has already been appended to u.
Note that it is better to use a StringBuilder to concatenate strings in a loop.

