1

I've been having problems running this program it compiles but doesn't run properly. When I run it and attempt to perform the calculations it spits out a bunch errors. I think it has to with variable types. Here is the program:

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

}
}

Here are the errors is printing out when attempting to perform the math (sorry it really long).

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1038)
at java.lang.Double.parseDouble(Double.java:548)
at area.actionPerformed(area.java:112)
...

Thanks so much in advance for an help!

1 Answer 1

2

When calling the functions:

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

either radi or sid is am Empty String, thats what

java.lang.NumberFormatException: empty String

tells you.
You might consider adding a System.out.println(raid + ", " + sid) before parsing to check what values are empty Strings and make sure, that the Strings are not empty.

Double.parseDouble(String s) throws a NumberFormatException when the given String s can not be parsed into a double value.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.