2

Somehow the output keeps ending up as completely wrong. Any way to fix this?

import java.util.Scanner;

public class MeterConversion {

    public static void main(String[] args) {


        int meters;
        System.out.print("Enter meters: ");
        Scanner userInput = new Scanner (System.in);
        meters = userInput.nextInt();

        double inches = (3.3 * 12 * meters);
        int feet = (int) (inches / 12);
        int miles = (feet / 5280);
        int milesConversion = (miles - (meters % miles));
        int feetConversion = ((miles - milesConversion) % feet);
        int inchesConversion = (int) (feetConversion % inches);

        System.out.print(meters + " meter(s) converts to " 
        + milesConversion +" mile(s), " + feetConversion + " feet, " 
        + inchesConversion + " inch(es)");

    }

}
Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
Crystal Data
  • 21
  • 1
  • 7
  • 1
    look up int division. – Hovercraft Full Of Eels Jan 27 '16 at 18:06
  • Please include an example (your input and the output) – z7r1k3 Jan 27 '16 at 18:08
  • @HovercraftFullOfEels isn't int division actually the point here, with OP wanting to convert to whole miles with remainder in feet and inches? I thought it would be appropriate to use int division to get from feet to whole miles. I suspect the problem is further down, with the `*Conversion` variables and trying to work out the leftover feet. – Jiri Tousek Jan 27 '16 at 18:24

1 Answers1

4

Try this:

import java.util.Scanner;

public class MeterConversion {

    public static void main(String[] args) {

        int meters;
        System.out.print("Enter meters: ");
        Scanner userInput = new Scanner (System.in);
        meters = userInput.nextInt();

        double inches = (39.370078 * meters);
        int miles = (int) (inches / 63360);
        int feet =  (int) (inches - miles * 63360) / 12;
        double inchesRemaining = inches - (miles*63360 + feet*12);

        System.out.print(meters + " meter(s) converts to " 
        + miles +" mile(s), " + feet + " feet, " 
        + inchesRemaining + " inch(es)");
    }
}

I wrote it this way so that it is easier to understand in terms of conversion from one unit to another. Hope it helps.

NeplatnyUdaj
  • 6,052
  • 6
  • 43
  • 76
Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
  • Perfect, thank you so much! I was under the impression that the modulus operation was the key to this, but it's much simpler than I though. ^.^ – Crystal Data Jan 27 '16 at 20:37
  • @CrystalData To learn a little more about using modulus for float/double check [here](http://stackoverflow.com/questions/2947044/how-do-i-use-modulus-for-float-double). A little googling will also help you a lot. – Sнаđошƒаӽ Mar 02 '16 at 03:31
  • 2
    I don't understand why anybody anybody would downvote that, it was exactly the answer I was looking for. :l But anyway, thanks for the help! Now it's at a net zero, lol. :P – Crystal Data Apr 13 '16 at 17:52