display number xxx as "x x x " method
posted 13 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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?
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?
posted 13 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
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
posted 13 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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..
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..
Do not wait to strike till the iron is hot; but make it hot by striking....
posted 13 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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.
posted 13 years ago
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
-
1 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
posted 13 years ago
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.
-
1 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
posted 13 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Thanks Rob Spoor for pointing out this....
Do not wait to strike till the iron is hot; but make it hot by striking....
posted 13 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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! )))
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! )))
posted 13 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.]
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.]
| I child proofed my house but they still get in. Distract them with this tiny ad: Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |












