• Post Reply Bookmark Topic Watch Topic
  • New Topic

display number xxx as "x x x " method  RSS feed

 
kambar bek
Greenhorn
Posts: 23
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i need to write method displayDigits that displays digits with 2 spaces between each digit

e.g. 4562 should be "4 5 6 2"

method accepts digits in range 1 - 99999, i have it done, but i implemented the code in a "stupid manner"

I have WORKING CODE , how do i refactor it to use while loop?

 
Karn Kumar
Ranch Hand
Posts: 153
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Erjan ,


I think no need to write this much code...

You can get that number and convert it to string with toString method .

And then you can apply while loop on condition of String legth and inside loop can use char at each position and append to new string with 2 spaces .

like pseudo code




-Chetan
 
Manoj Kumar Jain
Ranch Hand
Posts: 198
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can reduced this code to very simple code.
When you are printing the values you are getting the each digit and then printing each digit and space after that.
You have wrote lots of code just to fetch out the digit from the number.
you can simply do one thing, just change the int into a Sting and then iterate over it.

Hope this helps..
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would recommend you use a StringBuilder. Use the % and / operators to get the different digits from the int, and insert them at position 0 in the StringBuilder. Then, if the number is not 0, insert two spaces at position 0, too. Put the whole lot into a loop.
Beware: you will get peculiar results from negative number: -123 will spread out to 1  2  3

By the way: if you want to display spaces, either use code tags or the   character.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Erjan Kenjegalee wrote:I have WORKING CODE , how do i refactor it to use while loop?

Personally, I rather like the fact that you've adopted a numeric approach rather than rushing straight to Strings:
1. It's more generic.
2. It's likely to be faster (all number-to-String conversion methods are slow; number to char on the other hand, is blisteringly fast).
3. It has the potential to give you a by-product (the digits) which you can use as values elsewhere.

However, back to your question: How do you refactor it to use a while loop?

You clearly understand how '/' and '%' work, so how about just applying them progressively? (Hint: each '% 10' will give you the last digit, and each '/ 10' removes the last digit).

Give it a bit more thought and I'm sure you'll get there.

Winston
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manoj Kumar Jain wrote:

Please don't do that. Currently that's translated by the compiler into this:
That call to append will do the exact same as one single method call:
If you ever need to convert any primitive to a String, just use String.valueOf. It's overloaded so it can take any primitive, as well as objects. That will either return the String "null" or call toString() on the object.
 
Manoj Kumar Jain
Ranch Hand
Posts: 198
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob Spoor for pointing out this....
 
kambar bek
Greenhorn
Posts: 23
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks to everyone posted in this topic, i got it done, using NUMERIC approach, and using / and % as the exercise tells us

i needed to know how many digits the given number has, so i wrote the method for this.



again, thanks to all of you guys, one more exercise is done! )))
 
kambar bek
Greenhorn
Posts: 23
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i realized the naming for variables is not good enough, i used too many "number" in methods and variable names, (((

but it is ok, i m done anyways! ))

[Erjan Kenjegalee, please do not use profanity in your posts here. I have removed the offending word.]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!