# Java Program to Convert Kilometers(KMS) to Miles

Miles and kilometers are two different units for the measurement of distance. For the imperial system, we use miles and for the metric system, we use kms. Kilometers are used to measuring long distances whereas miles are used to compare relatively shorter distances.  We use this basic formula for the conversion of KMS to Miles:

``Distance in kms = 1.609 x distance in miles.``

If the user wants to perform the other way of this, which is the conversion of miles to kilometers, we use the formula below:

``Distance in miles = 0.621 x distance in kms.``

## Java program for conversion of Kilometers (KMS) to Miles

``````import java.util.Scanner;
public class DeveloperHelps {
public static void main(String[] args) {
double kilometers;
Scanner in = new Scanner(System.in);
kilometers = in.nextDouble();
double miles = kilometers / 1.609;
System.out.println(miles + " miles");
}
}``````

The output of the above program will be:

``````Please enter the kilometers
10
6.215040397762586 miles``````

In the program above, the user enters the KMS that he wants to convert. The KMS are then divided by 1.6 to get the miles. We use a scanner class to execute the program below.

## Java program for conversion of Miles to Kilometers (KMS)

``````import java.util.Scanner;
public class DeveloperHelps {
public static void main(String[] args) {
double miles;
Scanner in = new Scanner(System.in);
miles = in.nextDouble();
double kilometers = miles * 1.609;
System.out.println(kilometers + " Kilometers");
}
}``````

The output of this program will be:

``````Please enter the miles:
2
3.218 Kilometers``````

We can also make a single code for both this conversion of Miles to Kilometer and KMS to Miles using java. we use scanner class to design the following code. To understand that, refer the code below:

``````import java.util.Scanner;
public class DeveloperHelps {
public static void main(String[] args) {
System.out.print("Enter the distance in miles:");
Scanner s = new Scanner(System.in);
double distanceInMiles = s.nextDouble();
System.out.println(distanceInMiles + " miles = " + milesTokm(distanceInMiles) + " km");
System.out.print("Enter the distance in km:");
double distanceInKm = s.nextDouble();
System.out.println(distanceInKm + " km = " + kmTomiles(distanceInKm) + " miles");
s.close();
}
private static double milesTokm(double distanceInMiles) {
return distanceInMiles * 1.60934;
}
private static double kmTomiles(double distanceInKm) {
return distanceInKm * 0.621371;
}
}``````

The output of this program will be:

``````Enter the distance in miles:
2
3.218 kms
Enter the distance in kms:
5
3.107 miles``````

The method used is nextDouble() comes under java.util.Scanner package in java. It helps in scanning the upcoming next input in the code. If there is no text input, there will be an exception in the program which is NoSuchElementException. If the next input doesn’t match, the thrown exception is InputMismatchException. In case the scanner we closed, the exception is IllegalStateException. The input matches with the previous one and the code are able to run successfully. This method doesn’t take any inputs of its own and returns a double scanned input.