Conversion of kms to miles

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 measure 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;
kilometers = 10;
double miles = kilometers / 1.609;
System.out.println("Km to Miles: " +miles);

The output of the above program will be:

Km to Miles: 6.215040397762586

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.

Check out My Latest post on Developer Helps for some Interesting Insights

Java program for conversion of Miles to Kilometers (KMS)

import java.util.Scanner;
import java.util.NoSuchElementException;
public class MyClass { 
 public static void main(String args[]) {
		Scanner scanner = new Scanner(;
		try {
    		double miles;
            Scanner in = new Scanner(;
            System.out.println("Please enter the miles:");
            miles = in.nextDouble();
            double kilometers = miles * 1.609;
            System.out.println(kilometers + " Kilometers");
		} catch (NoSuchElementException e) {
		    System.out.println("Type something in the Stdin box above....");

The output of this program will be:

Please enter the miles:
3.218 Kilometers

Check Java 8 new features, training, and Certification details.

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 to 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(;
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");
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:

10.0 miles = 16.0934 km
20.0 km = 12.42742 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 is able to run successfully. This method doesn’t take any inputs of its own and returns a double-scanned input.

02 comments on “Java Program to Convert Kilometers(KMS) to Miles

  • Ekonomia Portal , Direct link to comment

    keep up the good work dude.thanks for your share and effort.better than other sites. whois

  • Chairs Furniture , Direct link to comment

    I enjoyed reading through your post. I really agree with what you wrote.

Leave a comment

Your email address will not be published. Required fields are marked *

Discover Our Exciting Courses and Quiz

Enroll now to enhance your skills and knowledge!

Java Online Quiz

Level up your coding skills with our interactive programming quiz!