The question is:
Write a code that will take and inches input and convert it to mm. Then display the same result using m, cm, and mm. Example would be:

Input:

53 inches

Output:

1346 mm
OR
1 m 34 cm and 6 mm


Something is happening with the remainder conversion that is not calculating the cm and mm right. I know I should probably using casting, but I'm not sure how to. Can someone help?

Thank you.

import java.util.Scanner;
import java.text.DecimalFormat;

public class Format
{
	public static void main (String[] args)
	{
		Scanner scan = new Scanner (System.in);
		
		System.out.print ("Enter a number in inches: ");
		double inches = scan.nextInt();
		
		double firstmm = inches * 25.4;
			
		
		double m = inches * 0.0254;
			inches = inches % 0.0254;
			
		double cm = inches * 100;
			inches = inches % 100;
			
		double mm = inches * 10;
			inches = inches % 10;
				 
			
		DecimalFormat fmt = new DecimalFormat ("0");
		
		System.out.println (fmt.format(firstmm) + "  mm");
		System.out.println ("OR");
		System.out.print(fmt.format (m) + " m ");
		System.out.print(fmt.format (cm) + " cm and ");
		System.out.print(fmt.format (mm) + " mm");
		}
}

conversion that is not calculating the cm and mm right.

please show us the program's output and explain what the problem is with it and show what you want the program to output.

The program is outputting

Enter a number in inches: 53
1346 mm
OR
1 m 2 cm and 0 mm


It should output:

1346 mm
OR
1 m 34 cm and 6 mm


Something is going wrong in the math after it converts from the inches to M.

Add some print statements to show the intermediate values of inches after each time you change its value. Compare what is printed out with what your manual computations show it should be.

I would suggest that instead of converting each time, use your already converted value.

For example, if I input 50 (pseudocode):

int mm=<calculate amount in mm>;

int m=mmcopy/1000;
mmcopy%1000;

And so on for all the other units.

This article has been dead for over six months. Start a new discussion instead.