Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove the comma after the last number in a loop [duplicate]

Tags:

java

this is my code

 System.out.println("Enter any integer number ");
int number = scan.nextInt();
while (number > 0) { System.out.print( number % 10 + ",");number = number / 10;}

To clarify if the user enter 1234 the output should be 4,3,2,1,

how can i edit the code to remove the comma after the last num? something like 4,3,2,1

like image 974
user3108110 Avatar asked Dec 25 '22 15:12

user3108110


1 Answers

Since everyone is having a field day, here's mine:

while (number > 0) {
    System.out.print( number % 10 + ((number/=10)>0 ? "," : ""));
}
like image 200
mprivat Avatar answered Dec 28 '22 06:12

mprivat