Armando Frias - 4 months ago 15

Java Question

To be more clear, please assist me with menuChoice == 2.

I've gotten to the point where if you enter negative values, it prompts you to enter again until it's positive and then the calculations come out fine. However, when I enter just positive values now, it doesn't calculate anything. I've been trying for a while but I just can't figure it out.

What do I need to do?

`package finalExam;`

//this is required for JOptionPane to work

import javax.swing.JOptionPane;

public class Geometry {

public static void main(String[] args) {

boolean valid = false;

int menuChoice;

do {

// create a menu and display it to the user

// then ask the user to choose an option

String menu = "1) Calculate the area of a circle\n"

+ "2) Calculate the area of a rectangle\n"

+ "3) Calculate the area of a triangle\n"

+ "4) Quit\n"

+ "Please enter your choice: (1, 2, 3, or 4)";

menuChoice = Integer.parseInt(JOptionPane.showInputDialog(menu));

if(menuChoice == 1)

{

String unknownRadius = JOptionPane.showInputDialog("What is the radius of the circle?");

if(Double.parseDouble(unknownRadius) < 0){

do{

JOptionPane.showMessageDialog(null, "Please enter positive numbers only.");

unknownRadius = JOptionPane.showInputDialog("What is the radius of the circle?");

}

while(Double.parseDouble(unknownRadius) < 0);

double knownRadius = Double.parseDouble(unknownRadius);

double circleArea = Math.pow(knownRadius, 2) * 3.14159;

JOptionPane.showMessageDialog(null, "The area of the circle is " + circleArea);

}

else if(Double.parseDouble(unknownRadius) > 0) {

double knownRadius = Double.parseDouble(unknownRadius);

double circleArea = Math.pow(knownRadius, 2) * 3.14159;

JOptionPane.showMessageDialog(null, "The area of the circle is " + circleArea);

valid = true;

}

} else if(menuChoice == 2){

String unknownLength = JOptionPane.showInputDialog("What is the length of the rectangle?");

if(Double.parseDouble(unknownLength) < 0){

do{

JOptionPane.showMessageDialog(null, "Please enter positive numbers only.");

unknownLength = JOptionPane.showInputDialog("What is the length of the rectangle?");

}

while(Double.parseDouble(unknownLength) < 0);

double knownLength = Double.parseDouble(unknownLength);

String unknownWidth = JOptionPane.showInputDialog("What is the width of the rectangle?");

if(Double.parseDouble(unknownWidth) < 0){

do{

JOptionPane.showMessageDialog(null, "Please enter positive numbers only.");

unknownWidth = JOptionPane.showInputDialog("What is the width of the rectangle?");

}

while(Double.parseDouble(unknownWidth) < 0);

double knownWidth = Double.parseDouble(unknownWidth);

double rectangleArea = knownLength * knownWidth;

JOptionPane.showMessageDialog(null, "The area of the rectangle is " + rectangleArea);

}

else if(Double.parseDouble(unknownLength) > 0){

knownLength = Double.parseDouble(unknownLength);

unknownWidth = JOptionPane.showInputDialog("What is the width of the rectangle?");

if(Double.parseDouble(unknownWidth) > 0) {

double knownWidth = Double.parseDouble(unknownWidth);

double rectangleArea = knownLength * knownWidth;

JOptionPane.showMessageDialog(null, "The area of the rectangle is " + rectangleArea);

valid = true;

}

}

}

} else if(menuChoice == 3){

String unknownBase = JOptionPane.showInputDialog("What is the base length of the triangle?");

if(Double.parseDouble(unknownBase) > 0){

double knownBase = Double.parseDouble(unknownBase);

String unknownHeight = JOptionPane.showInputDialog("What is the height of the triangle?");

if(Double.parseDouble(unknownHeight) > 0){

double knownHeight = Double.parseDouble(unknownHeight);

double triangleArea = (knownBase / 2) * knownHeight;

JOptionPane.showMessageDialog(null, "The area of the triangle is " + triangleArea);

valid = true;

}

else { JOptionPane.showMessageDialog(null, "Please enter a positive number");

JOptionPane.showInputDialog("What is the base length of the triangle?");

}

}

else { JOptionPane.showMessageDialog(null, "Please enter a positive number");

JOptionPane.showInputDialog("What is the height of the triangle?");

}

}else if(menuChoice == 4){

System.exit(0);

} else

JOptionPane.showMessageDialog(null, "Please select from the options given (1-4)!");

}

while(!valid || menuChoice != 4);

}

}

Answer

You've made things too complicated for yourself by having two different blocks of code depending on whether the *first* length is negative or positive. Basically, your code looks like:

```
If the length is negative then {
Ask for a new length until it's positive
Now input the width, reject negative values, do the computation,
and output it
} else { // the length is positive
Input the width, reject negative values, do the computation,
and output it
}
```

The whole part about handling the width occurs twice in your code, which makes the code needlessly complex, and makes it more prone to errors such as getting the curly braces in the wrong place (which I think is why the code isn't behaving). You can make life a lot simpler for yourself by reorganizing the code like this:

```
If the length is negative then {
Ask for a new length until it's positive
}
// When we get here, the length will be positive. It doesn't matter
// whether we got here because the original length was positive, or whether
// it was negative and the user entered a new value. We're going to
// continue in the same way, either way.
Input the width, reject negative values, do the computation,
and output it
```

(By the way, I don't know what you want to do if the user enters 0. You really aren't handling that case.)