1

我在运行它编译但无法正常运行的程序时遇到问题。当我运行它并尝试执行计算时,它会吐出一堆错误。我认为它必须与变量类型有关。这是程序:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class area extends JFrame implements ActionListener, ItemListener{

 //row 1
  JPanel row1 = new JPanel();
  JLabel select = new JLabel("Please select what you would like to caculate the area and volume of.");
 //row 2
  JPanel row2 = new JPanel();
  JCheckBox circle = new JCheckBox("Circle", false);
  JCheckBox cube = new JCheckBox("Cube", false);
 //row 3 
  JPanel row3 = new JPanel();
  JLabel radlab = new JLabel("Radius of the circle (in cm)");
  JTextField rad = new JTextField(3);
  JLabel sidelab = new JLabel("A side of the cube (in cm)");
  JTextField side = new JTextField(3); 
//row4
  JPanel row4 = new JPanel(); 
  JButton solve = new JButton("Solve!");
//row 5
  JPanel row5 = new JPanel();
  JLabel areacallab = new JLabel("Area");
  JTextField areacal = new JTextField(10);
  JLabel volumelab = new JLabel("Volume");
  JTextField volume = new JTextField(10);
public area(){
  setTitle("Area Caculator");
    setSize(500,400);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
//disables all text areas 
  rad.setEnabled(false);
  side.setEnabled(false);
  areacal.setEnabled(false);
  volume.setEnabled(false);
//add listeners
  circle.addItemListener(this);
  cube.addItemListener(this);
  solve.addActionListener(this);
FlowLayout one = new FlowLayout(FlowLayout.CENTER);
   setLayout(one);

  row1.add(select);
   add(row1);


  row2.add(circle);
  row2.add(cube);
   add(row2);

  row3.add(radlab);
  row3.add(rad);
  row3.add(sidelab);
  row3.add(side);
   add(row3);

  row4.add(solve);
   add(row4);

  row5.add(areacallab);
  row5.add(areacal);
  row5.add(volumelab);
  row5.add(volume);
   add(row5);

}
public void circlepick(){
  //cube.setCurrent(false);
  cube.setEnabled(false);
  rad.setEnabled(true); 
}
public void cubepick(){
  circle.setEnabled(false);
  side.setEnabled(true);
}
@Override
public void itemStateChanged(ItemEvent event) {
  Object item = event.getItem();
    if (item == circle){
      circlepick();
    }
    else if (item == cube){
      cubepick();
    }
  }
@Override
public void actionPerformed(ActionEvent evt){
  //String radi = rad.getText();
  //String sid = side.getText(); 

   //circlesolve();
   //cubesolve();
String radi = rad.getText();
String sid = side.getText(); 
double radius = Double.parseDouble(radi);
double length = Double.parseDouble(sid);

  double cirarea = Math.PI * Math.pow(radius, 2);
  double cirvolume =  (4.0 / 3) * Math.PI * Math.pow(radius, 3);
  double cubearea = Math.pow(length, 2);
  double cubevolume = Math.pow(length, 3);
  areacal.setText("" + cirarea + cubearea + "");
  volume.setText("" + cirvolume + cubevolume + "");
}
public static void main(String[] args) {
    area are = new area(); 

}
}

这是尝试执行数学运算时打印出的错误(对不起,它真的很长)。

线程“AWT-EventQueue-0”java.lang.NumberFormatException 中的异常:区域 的 java.lang.Double.parseDouble(Double.java:548) 处
的 sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1038)处的空字符串.actionPerformed(area.java:112) ...


非常感谢您的帮助!

4

1 回答 1

2

调用函数时:

double radius = Double.parseDouble(radi);
double length = Double.parseDouble(sid);

要么 要么radisidString, 就是这样

java.lang.NumberFormatException: empty String

告诉你。
您可能会考虑System.out.println(raid + ", " + sid)在解析之前添加一个以检查哪些值是空字符串并确保字符串不为空。

Double.parseDouble(String s)NumberFormatException当给定的String s不能被解析成一个double值时抛出一个。

于 2013-03-25T02:31:15.210 回答