This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.

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.

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.)

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.

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 fivearithmetic operators available in all the C-based languages (Java included).

Randy Woods
Greenhorn

Joined: Sep 08, 2010
Posts: 3

posted

0

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 fivearithmetic 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

Joined: Oct 13, 2005
Posts: 44545

34

posted

0

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.