• Post Reply Bookmark Topic Watch Topic
  • New Topic

Converting long distances/times in seconds to seperate parts in a String  RSS feed

 
Mark Shrauve
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
 
Campbell Ritchie
Marshal
Posts: 55768
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 55768
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and sorry for posting twice, but if you want a String you can use
 
jiju ka
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!