| Author |
Converting long distances/times in seconds to seperate parts in a String
|
Mark Shrauve
Greenhorn
Joined: Oct 09, 2005
Posts: 4
|
|
Hey Ive had an assignment for a huge program and I conquered it all well but once again, I get caught on one of the simplest parts because we weren't really taught how to actually do this. So say you have a distance of 325 inches. I then would convert that to feet by dividing it by 12 and getting 27.083. I have no clue how to return that value as a String and tell the program that what's before the decimal is the feet (the 27) and whats after the decimal (the .08 which I have to also tell the program to round) are the inches. I also have to do this with time as well to do this with time so lets say the longest time is 60.3 seconds. I have no idea how to convert that to each seperate part as well as in 1 minute and .03 seconds. Thanks alot I really do appreciate it (I thought of using a String tokenizer or a split method but again, we were never taught that in my class so why would we use it? I know it has to be more simple)
|
 |
jiju ka
Ranch Hand
Joined: Oct 12, 2004
Posts: 302
|
|
If needed String.length() can be used to find length and trim can be used to trim off white spaces. There are other ways to accomplish the same.
|
 |
jiju ka
Ranch Hand
Joined: Oct 12, 2004
Posts: 302
|
|
|
Ideally you have to Formatter objects to do this. Since StringTokenizer is not in your study yet, Formatters will be more advanced to you.
|
 |
Mark Shrauve
Greenhorn
Joined: Oct 09, 2005
Posts: 4
|
|
Originally posted by jiju ka: If needed String.length() can be used to find length and trim can be used to trim off white spaces. There are other ways to accomplish the same.
Hey I wasnt sure of how to exactly use the indexof method and the substring. Did I get anything right because I still am getting errors.
|
 |
Jaime M. Tovar
Ranch Hand
Joined: Mar 28, 2005
Posts: 133
|
|
I will do it like this: first we have a 325 inches, we divide by 12 and get 27.0833333 we assign it to a value but only the 27 part. Then i will multiply the 27 * 12 and get 324, then we substract 325 - 324 and get 1, this is the inches part double a = 325; int f = (int) a / 12; int in = (int) a - (f * 12);
|
She will remember your heart when men are fairy tales in books written by rabbits.<br /> As long as there is duct tape... there is also hope.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32704
|
|
Jim Tovar writes,
first we have a 325 inches, we divide by 12 and get 27.0833333 we assign it to a value but only the 27 part. Then i will multiply the 27 * 12 and get 324, then we substract 325 - 324 and get 1, this is the inches part double a = 325; int f = (int) a / 12; int in = (int) a - (f * 12);
Your method will work, but it is quicker to declare the a variable as an int, then find the feet using a / 12 and the inches using a % 12. Beware of repeated dividing and multiplying, otherwise one day you will get 0.0833333 * 12 = 0.9999996, which when cast to an int comes out as 0.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32704
|
|
. . . and sorry for posting twice, but if you want a String you can use
|
 |
jiju ka
Ranch Hand
Joined: Oct 12, 2004
Posts: 302
|
|
Two algorithms; second one is theoretically true(you need to do something else - [casting] to yield right answer). See which suites your purpose.
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
Maybe ... This could grow with more constructors for feet & inches, conversions to yards, miles, furlongs, whatever you need. For grins, google "123 inches in feet". They don't break down the anwwer you want, tho. [ November 01, 2005: Message edited by: Stan James ]
|
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
|
 |
 |
|
|
subject: Converting long distances/times in seconds to seperate parts in a String
|
|
|