Reputation: 3
I'm trying to write a loop where I have the user input a value for meters and then based on the menu given they will choose an option that will print out the conversion, it should run until the user inputs 4 to exit the program. However my program is unable to run each loop out of order or more than once and I'm not sure why exactly. I am aware of switches and breaks however my teacher discourages us from doing anything we have yet to do as a class.
import javax.swing.JOptionPane;
public class Conversion_LAbA {
public static void main(String[] args) {
// TODO Auto-generated method stub
//Junior Nieves Alvarez, Conversion LabA
String inputString, inputOption;
double meters, kilometers, inches, feet;
int option;
inputString = JOptionPane.showInputDialog("Enter the Meters value");
meters = Double.parseDouble(inputString);
if (meters < 1)
{
inputString =JOptionPane.showInputDialog("Enter the Meters value");
}
System.out.println("Choose one of the Following"
+ " 1. Convert to Kilometers"
+ " 2. Convert to Inches"
+ " 3. Convert to Feet"
+ " 4. Exit");
inputOption = JOptionPane.showInputDialog("Choose an Option");
option = Integer.parseInt(inputOption);
while (option == 1)
{
kilometers = meters*0.001;
System.out.println(kilometers);
inputOption = JOptionPane.showInputDialog("Choose an Option");
option = Integer.parseInt(inputOption);
}
while (option == 2)
{
inches = meters*39.37;
System.out.println(inches);
inputOption = JOptionPane.showInputDialog("Choose an Option");
option = Integer.parseInt(inputOption);
}
while (option == 3)
{
feet= meters*3.281;
System.out.println(feet);
inputOption = JOptionPane.showInputDialog("Choose an Option");
option = Integer.parseInt(inputOption);
}
if (option == 4)
{
System.out.println("The program will end");
System.exit(0);
}
}
}
Upvotes: 0
Views: 4428
Reputation: 2777
no breaks, no switches, with some validation
package com.qvs;
import javax.swing.JOptionPane;
public class Conversion_LAbA {
public static void main(String[] args) {
// TODO Auto-generated method stub
//Junior Nieves Alvarez, Conversion LabA
String inputString = "", inputOption;
double meters;
int option;
for (;;) {
inputString = JOptionPane.showInputDialog("Enter the Meters value");
meters = Double.parseDouble(inputString);
if (meters >= 1) {
System.out.println("Choose one of the Following"
+ " 1. Convert to Kilometers"
+ " 2. Convert to Inches"
+ " 3. Convert to Feet"
+ " 4. Exit");
option = 0;
while ((option < 1) || (option > 4)) {
inputOption = JOptionPane.showInputDialog("Choose an Option");
option = Integer.parseInt(inputOption);
if (option == 1) {
System.out.println(meters * 0.001);
} else if (option == 2) {
System.out.println(meters * 39.37);
} else if (option == 3) {
System.out.println(meters * 3.281);
} else if (option == 4) {
System.out.println("The program will end");
System.exit(0);
} else {
System.out.println("Invalid option");
}
}
} else {
System.out.println("Meters entered must be >= 1");
}
}
}
}
Upvotes: 0
Reputation: 186
If you have a while loop on each of those options, the compiler is going to continue running that loop until the number changes. The number won't change, so the state will just stay the same.
You do need a switch statement, or a series of IF-ELSE statements. I had a teacher before that had us do the IF-ELSE statements to show us how cumbersome it becomes with many options. Then they gave us the switch statement.
Upvotes: 0
Reputation: 4099
There were a couple issues with your logic.
You want to wrap your input for meters in a while loop, that way you make sure the meters entered by the user will be valid for the remainder of your program's execution.
All there is left to do is wrap your options in the while loop with if statements instead of while loops for each. This way you can select an option and then be prompted to try a different conversion within a run of the program and be able to enter your options in any order you want.
Lastly it is helpful to put the measurement symbol next to the output just so when the user selects an option they can see what conversion they were using, km for kilometers, " for inches and ' for feet.
String inputString, inputOption;
double meters = 0, kilometers, inches, feet;
int option = 0;
while(meters < 1)
{
inputString = JOptionPane.showInputDialog("Enter the Meters value");
meters = Double.parseDouble(inputString);
if (meters < 1)
{
System.out.println("Invalid Meter value, please enter a value >= 1");
option = 0;
}
}
while(option != 4){
System.out.println("Choose one of the Following"
+ "\n 1. Convert to Kilometers"
+ "\n 2. Convert to Inches"
+ "\n 3. Convert to Feet"
+ "\n 4. Exit");
inputOption = JOptionPane.showInputDialog("Choose an Option");
option = Integer.parseInt(inputOption);
if(option == 1)
{
kilometers = meters*0.001;
System.out.println(kilometers + "km");
}
else if(option == 2)
{
inches = meters*39.37;
System.out.println(inches + "\"");
}
else if(option == 3)
{
feet= meters*3.281;
System.out.println(feet + "'");
}
else if (option == 4)
{
System.out.println("The program will end");
}
}
Upvotes: 1