This week's book giveaway is in the Java in General forum.We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!

# Using Java to convert mm to m, cm and mm

Randy Woods
Greenhorn
Posts: 3
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

David Newton
Author
Rancher
Posts: 12617
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.)

Rob Spoor
Sheriff
Posts: 20527
54
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
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
Rancher
Posts: 12617
Divide?

Campbell Ritchie
Sheriff
Posts: 48917
58
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
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
Sheriff
Posts: 48917
58
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.