• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using Java to convert mm to m, cm and mm

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I am taking a Java class and am stuck on a problem. I had write a program to convert inches to mm and then convert the mm to m, cm, and mm. I have been able to convert the inches to mm successfully. My problem is when it comes down to converting the mm to m, cm and mm, all in a group, I have no idea on how to do it. If someone could point me in the right direction, I would appreciate it.

Randy
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

I'm not sure what you mean by "all in a group", but converting from mm to anything else in the metric system is pretty easy--just divide by the appropriate number. (And I don't know if I'd bother converting mm to mm.)
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Go to your JDK installation folder. Open the src.zip file. Extract the java/util/concurrent/TimeUnit.java file. Check out how that works.
 
Randy Woods
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:Welcome to JavaRanch!

I'm not sure what you mean by "all in a group", but converting from mm to anything else in the metric system is pretty easy--just divide by the appropriate number. (And I don't know if I'd bother converting mm to mm.)




Here's an example of what I mean. After asking the user to enter inches, and the user enters 53.

My program converts it to 1346 mm.

I now need the program to convert the 1346 mm to output:

1 m, 34 cm, 6, mm.

This is where I am lost at. I can't figure out how to get it to do this.

If it makes any difference, I am using Netbeans IDE.

Thanks,

Randy
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Divide?
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pencil and paper. That is the answer. Never mind this new-fangled electronic stuff. That might give you the answer very fast, but only pencil and paper will give you the algorithm. That's what I keep telling our undergraduates, and a few eventually get the message. You also need an eraser. That is far and away the most important piece of hardware required. So get a large one.

Joking aside, you need to write down very simply on a sheet of paper how you are going to get the answer (by the way, 1m 34cm 6mm is a very unusual way to express a length). When I say simply I mean simply: get it down to words of one syllable. Now you have pseudo-code, which enables you to work out the real code easily.
53in = 1346.2mm, so how are you getting 1346? Your arithmetic appears correct, but how are you converting it to a whole number?
Remind yourself of the five arithmetic operators available in all the C-based languages (Java included).
 
Randy Woods
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Pencil and paper. That is the answer. Never mind this new-fangled electronic stuff. That might give you the answer very fast, but only pencil and paper will give you the algorithm. That's what I keep telling our undergraduates, and a few eventually get the message. You also need an eraser. That is far and away the most important piece of hardware required. So get a large one.

Joking aside, you need to write down very simply on a sheet of paper how you are going to get the answer (by the way, 1m 34cm 6mm is a very unusual way to express a length). When I say simply I mean simply: get it down to words of one syllable. Now you have pseudo-code, which enables you to work out the real code easily.
53in = 1346.2mm, so how are you getting 1346? Your arithmetic appears correct, but how are you converting it to a whole number?
Remind yourself of the five arithmetic operators available in all the C-based languages (Java included).



Thanks. I now understand that I have to use the Modulus operator to get the results that I need. I really appreciate you pointing me to the right direction.

Randy
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome

In this country (UK) we call it the remainder operator, in most non-English-speaking countries it is called modulus, and when I last did any maths, modulus (in England) meant absolute value |-123| = 123.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i want the solution of the conversion 1.6978 mm into in

http://mm-to-inches.net/
 
reply
    Bookmark Topic Watch Topic
  • New Topic