2016-10-13 7 views
-1

siehe bitte meinen Code:Wie behebe ich diese java.lang.NumberFormatException?

Wie behebe ich den Fehler, den ich von Eclipse empfange?

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

} 
} 

Dies ist der Fehler:

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) 
+3

Sie versuchen 'menu' als eine ganze Zahl zu analysieren? Wie erwarten Sie, dass das funktioniert? – Li357

+0

Ich denke, du musst ein paar Grundlagen über Java und Swing lernen. Hier ist ein [Beispiel für JOptionPane] (http://www.javabeginner.com/java-joptionpane-class-example) – JimHawkins

Antwort

2

Ich glaube, das, was Sie versuchen zu tun ist:

// 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 returns a string of the input, so that is what you want to parse*/ 
/*also you may want to add a try catch to prevent crashes, should the user not input a valid integer*/ 
menuChoice = Integer.parseInt(JOptionPane.showInputDialog(menu)); 

if(menuChoice == 1) 
{ 

Was Sie wurden ursprünglich zu tun versucht, eine ganze Zahl zu analysieren von Ihrem Menütext [1] Berechne den Bereich ...]

4

Sie vermissen die Option Antwort in einem String zu erfassen:

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

Oder analysieren es direkt:

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