This week's book giveaways are in the Cloud and AI/ML forums.
We're giving away four copies each of Cloud Native Patterns and Natural Language Processing and have the authors on-line!
See this thread and this one for details.
Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

display number xxx as "x x x " method

 
Greenhorn
Posts: 23
Eclipse IDE Python Java
  • 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?

 
Ranch Hand
Posts: 153
Eclipse IDE Tomcat Server Java
  • 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
 
Ranch Hand
Posts: 198
Oracle Java Linux
  • 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..
 
Marshal
Posts: 65002
246
  • 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.
 
Bartender
Posts: 10771
71
Hibernate Eclipse IDE 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
 
Sheriff
Posts: 21773
103
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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
Oracle Java Linux
  • 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 Python Java
  • 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 Python Java
  • 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.]
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!