Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I am new to JAVA and need some serious help, I need to create a simple program that will give a user the option of converting miles to kilometers or kilometers to miles. I have a lot of starts but I can not seem to get very far. Please if anyone has an example?

To convert miles to kilometers multiply by 1.609344. To convert kilometers to miles multiply by 0.6213711.
The rest is easy.
Stuart

Thank you very much. Unfortunately, I already have that information : ) I am actually stuck on the easy parts. I honestly am confused at the moment as to how to create the program that produces those conversions. Thankyou again. I very much appreciate your quick response!

"have a lot of starts but I can not seem to get very far"
What do you have sp far?
If at first you don't succeed, you're about average.M2

Here is what I have. Please any ideas?
import java.util.*;
public class winchesterProject1
{
public static void main(String[] args)
{
System.out.println("This program will convert miles to kilometers or kilometers to miles.");
int n1;
int enter = 0;
double miles, kilometers;
String answer;
Scanner scannerObject = new Scanner(System.in);
do
{
System.out.println("Enter a number you would like to convert:");
n1 = scannerObject.nextInt();
{
System.out.println("Enter 1 to convert this number to kilometers.");
System.out.println("Enter 2 to convert this number to miles.");
enter = 0;
enter = scannerObject.nextInt();
}
while (n1 > 0)
{
if (enter = 1)
kilometers = n1 * 1.609344;
else if (enter = 2)
miles = n1 * .0621371;
else
System.out.println("Please try again.");
}
System.out.println("Would you like to convert another distance?");
System.out.println("Enter yes or no.");
answer = scannerObject.next();
while (answer.equalsIgnoreCase("yes"));
}
}
}
}
}

I've noticed several problems:
* You have too many closing braces. The program shouldn't even compile with all those extra braces. Always make sure you have the same number of opening braces as closing braces.
* The WHILE part of the do/while loop should be right after the closing brace for that structure.
* I'm not sure why you have a brace set beginning after the line n1 - scannerObject.nextInt(); and ending before the line while (n1 > 0). Why did you add these in?
* To test for equality you need two = signs (ie: if (enter == 0){})
I've fixed these problems since they are pretty simple and are issues with syntax, not logic. Here's is the code:
import java.util.*;
public class winchesterProject1
{
public static void main(String[] args)
{
System.out.println("This program will convert miles to kilometers or kilometers to miles.");int n1;
int enter = 0;
double miles, kilometers;
String answer;
Scanner scannerObject = new Scanner(System.in);do
{
System.out.println("Enter a number you would like to convert:");
n1 = scannerObject.nextInt();
System.out.println("Enter 1 to convert this number to kilometers.");
System.out.println("Enter 2 to convert this number to miles.");
enter = 0;
enter = scannerObject.nextInt();while (n1 > 0)
{
if (enter == 1)
kilometers = n1 * 1.609344;
else if (enter == 2)
miles = n1 * .0621371;
else
System.out.println("Please try again.");
}System.out.println("Would you like to convert another distance?");
System.out.println("Enter yes or no.");
answer = scannerObject.next();
} while (answer.equalsIgnoreCase("yes"));
}
}Now for the bad news. You have some logic problems in your code including an infinite loop. I haven't fixed those problems because if you find the mistakes yourself you will be less likely to make them again later on. If after working on it you still have problems post back.

Thank you very much, I was having a heck of a time placing the while loop. I added a break statement after the final else and it seems to be working fine now Thank you again!!!! : )

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |