• Post Reply Bookmark Topic Watch Topic
  • New Topic

Comma Separated Number  RSS feed

 
B Travis
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm a Java novice. I need to to ask the user to enter a digit between 1000 and 1000000. I then need to print this entered number with commas separating the thousands and millions. I've managed to get some of the way there but I can't get the millions comma separator? I've tried adding a third substring but it doesn't work. Any help appreciated. Code below:


 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You need to make it conditional. Remember that the "third part" (and also the "second comma") may not even exist.

Henry
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Are you required to add the commas yourself? Do you know how to write a loop which will keep going, three at a time, until you have exhausted the numbers? Are you allowed to use the %d tag with the , option in printf?
If you are using Java8, consider using a StringJoiner object to add the individual Strings separated by commas.
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you can use NumberFormat.format to display the user input. Or is that not allowed?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!