Armando Frias - 1 year ago 73

Java Question

please see my code below:

How do I resolve the error I'm receiving from eclipse?

`package finalExam;`

//this is required for JOptionPane to work

import javax.swing.JOptionPane;

//this allows for exception throwing

import java.io.*;

public class Geometry {

public static void main(String[] args) throws IOException {

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)";

JOptionPane.showInputDialog(menu);

menuChoice = Integer.parseInt(menu);

if(menuChoice == 1)

{

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

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?");

double knownLength = Double.parseDouble(unknownLength);

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

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?");

double knownBase = Double.parseDouble(unknownBase);

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

double knownHeight = Double.parseDouble(unknownHeight);

double triangleArea = (knownBase / 2) * knownHeight;

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

valid = true;

}else if(menuChoice == 4){

System.exit(0);

}else if(menuChoice > 0)

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

}

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

}

}

This is the error:

`Exception in thread "main" java.lang.NumberFormatException: For input string: "1) Calculate the area of a circle`

2) Calculate the area of a rectangle

3) Calculate the area of a triangle

4) Quit

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

at java.lang.NumberFormatException.forInputString(Unknown Source)

at java.lang.Integer.parseInt(Unknown Source)

at java.lang.Integer.parseInt(Unknown Source)

at finalExam.Geometry.main(Geometry.java:25)

Answer Source

It's my standard answer to such questions:

```
Exception in thread "main" java.lang.NumberFormatException: For input string: "1) Calculate the area of a circle
2) Calculate the area of a rectangle
3) Calculate the area of a triangle
4) Quit
Please enter your choice: (1, 2, 3, or 4)"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at finalExam.Geometry.main(Geometry.java:25)
```

means:

```
There was an error. We try to give you as much information as possible
It was an Exception in main thread. It's called NumberFormatException and has occurred for input "1) Calculate the area of a circle
2) Calculate the area of a rectangle
3) Calculate the area of a triangle
4) Quit
Please enter your choice: (1, 2, 3, or 4)"
at line 65th of NumberFormatException.java which is a constructor,
which was invoked from Integer.parseInt() which is in file Integer.java in line 580,
which was invoked from Integer.parseInt() which is in file Integer.java in line 615,
which was invoked from method main in file Geometry.java in line 25.
```

In other words, you tried to parse thw whole menu to an `int`

what Java can't do with method `Integer.parseInt`

. Java has provided beautiful stacktrace which tells you exactly what the problem is. The tool you're looking for is **debugger** and using **breakpoints** will allow you to inspect the *state* of you application at the chosen moment.

The solution might be the following logic:

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

Why? `JOptionPane`

shows an input dialog and then returns the value which is entered to the String. So there are two possibilities. Either 'catch' the `String`

:

```
String userChoice = JOptionPane.showInputDialog(menu);
menuChoice = Integer.parseInt(userChoice);
```

or directly parse as I have shown before. The first option is better in case you don't care about the alphanumerical value of the input, the second if you do.

Remember, that as you has seen, `Integer#parseInt`

throws `NumberFormatException`

so you should catch it. Why? Because you can't rely on the user giving the correct input. User's mistake can't crash your application.

Finally, knowing the above, the code should look like the following:

```
try {
menuChoice = Integer.parseInt(JOptionPane.showInputDialog(menu));
} catch (NumberFormatException e) {
menuChoice = 5; //add logic for wrong input. In case menuChoice = 5,
// say... ask for another input
}
```