943,712 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 601
  • Java RSS
Sep 7th, 2009
0

a loop that extracts each digit of a number

Expand Post »
i want to extract

576

to

hundreds = 5
tens = 7
ones = 6

my code does this purpose, but..
java Syntax (Toggle Plain Text)
  1. h=num/100;
  2. t=(num-(h*100))/10;
  3. o=((num-(h*100))-(t*10));

i want to rewrite my code using loop and modulus operator..

help guys. thanks

btw: i'm writing a program that converts numbr to w.ord-/.
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
scias23 is offline Offline
69 posts
since Jan 2009
Sep 7th, 2009
0

Re: a loop that extracts each digit of a number

Convert integer number to String. Length of of this String gives you info, how many digits are. Create int[string.length()] array. Use loop to transcribe all values. On fly convert particular values to int.
Find a better solution (using % and / operators).
Last edited by quuba; Sep 7th, 2009 at 3:17 am. Reason: added last line
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Sep 7th, 2009
0

Re: a loop that extracts each digit of a number

@scias23 : if I can't get the code from working, pause for a while and ask yourself, "am i coding the right code?"
-- while!
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Sep 7th, 2009
0

Re: a loop that extracts each digit of a number

i can't think of a solution that uses the modulo operator

Click to Expand / Collapse  Quote originally posted by quuba ...
Convert integer number to String. Length of of this String gives you info, how many digits are. Create int[string.length()] array. Use loop to transcribe all values. On fly convert particular values to int.
Find a better solution (using % and / operators).
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
scias23 is offline Offline
69 posts
since Jan 2009
Sep 7th, 2009
0

Re: a loop that extracts each digit of a number

Hi!
I suggest you should read on this and you will find the solution easily..
But as a help,
You can use % operator to simplify do it.
Java Syntax (Toggle Plain Text)
  1. int a=563%100; //a=63
  2. int b=a%10; //b=3
use % with / and you can simply do the job...

I hope this helps you..
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tha_ratl is offline Offline
7 posts
since Aug 2009
Sep 7th, 2009
0

Re: a loop that extracts each digit of a number

Laboratory:
java Syntax (Toggle Plain Text)
  1. public class Convert {
  2.  
  3. //1. base form
  4. static int[] convert(int k) {
  5. int[] result = null;
  6. return result;
  7. }
  8.  
  9. static int[] convert2(int k) {
  10. int size = 3;//arbitrary
  11. int[] result = new int[size];
  12. return result;
  13. }
  14.  
  15. static int[] convert3(int k) {
  16. int size = 8;
  17. int[] result = new int[size];
  18. for (int i = 0; i < size; i++) {
  19. result[i] = k % 10;
  20. // what can i do with k?
  21. }
  22.  
  23. return result;
  24. }
  25.  
  26. public static void main(String[] args) {
  27. int k = 576;
  28. int[] result = convert(k);
  29. //int[] result = convert2(k);
  30. //int[] result = convert3(k);
  31. System.out.println(k);
  32. System.out.println(result[0]);
  33. System.out.println(result[1]);
  34. System.out.println(result[2]);
  35. //System.out.println(result[3]);
  36. }
  37. }
array is a good place, to store calculated values.
Write once, use many times.
Continue...
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Sep 7th, 2009
0

Re: a loop that extracts each digit of a number

Quote ...
i'm writing a program that converts numbr to w.ord-/.
If your final purpose is this then I think it is more simple to convert the no to string and then process it.. It is straight forward.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tha_ratl is offline Offline
7 posts
since Aug 2009
Sep 7th, 2009
0

Re: a loop that extracts each digit of a number

i'm writing a program that converts numbers to words.. from 0-9999. it's inefficient to convert the number to string for this purpose.

Click to Expand / Collapse  Quote originally posted by tha_ratl ...
If your final purpose is this then I think it is more simple to convert the no to string and then process it.. It is straight forward.
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
scias23 is offline Offline
69 posts
since Jan 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Load data infile *.abc dynamically
Next Thread in Java Forum Timeline: javax.comm.ComPortIdentifier





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC