File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Using Java to convert mm to m, cm and mm Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using Java to convert mm to m, cm and mm" Watch "Using Java to convert mm to m, cm and mm" New topic
Author

Using Java to convert mm to m, cm and mm

Randy Woods
Greenhorn

Joined: Sep 08, 2010
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

Joined: Sep 29, 2008
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

Joined: Oct 27, 2005
Posts: 19685
    
  20

Go to your JDK installation folder. Open the src.zip file. Extract the java/util/concurrent/TimeUnit.java file. Check out how that works.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Randy Woods
Greenhorn

Joined: Sep 08, 2010
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

Joined: Sep 29, 2008
Posts: 12617

Divide?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
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

Joined: Sep 08, 2010
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

Joined: Oct 13, 2005
Posts: 38519
    
  23
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using Java to convert mm to m, cm and mm