Search code examples
javaeclipseswingeventsequation-solving

How would I have my program calculate two equations from one input field?


I need two functions to be calculated from the user and wanted to know how I would use the same input field to display in an dialog box both answers when it is done. I am in the fourth week of my comp sci class and I feel like I have dove into a bit of trouble. We were asked to do this program using the JOptionPane but I wanted to do a little more and created my own.

What I have done so far, the program has compiled correctly in JGrasp and Eclipse but shows this when I run the application.

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842)
    at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
    at java.lang.Double.parseDouble(Double.java:538)
    at Nicolas_Carabajal_Assignment3$CalcButtonListener.actionPerformed(Nicolas_Carabajal_Assignment3.java:57)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6525)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3322)
    at java.awt.Component.processEvent(Component.java:6290)
    at java.awt.Container.processEvent(Container.java:2234)
    at java.awt.Component.dispatchEventImpl(Component.java:4881)
    at java.awt.Container.dispatchEventImpl(Container.java:2292)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
    at java.awt.Container.dispatchEventImpl(Container.java:2278)
    at java.awt.Window.dispatchEventImpl(Window.java:2739)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:751)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:702)
    at java.awt.EventQueue$3.run(EventQueue.java:696)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:724)
    at java.awt.EventQueue$4.run(EventQueue.java:722)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:721)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

The Code:

 /**
 * 
 *@author ngc5043
 *@version 1.0 
 */

import javax.swing.*;
import java.awt.event.*;

public class Nicolas_Carabajal_Assignment3 extends JFrame
{
private JPanel panel;
private JLabel messageLabel;
private JTextField ExTextField;
private JButton calcButton;
private final int WINDOW_WIDTH = 310;
private final int WINDOW_HEIGHT = 100;


 public static void main(String[] args)
  {
      Nicolas_Carabajal_Assignment3 main = new Nicolas_Carabajal_Assignment3();
      main.buildPanel();
  }
  public Nicolas_Carabajal_Assignment3()
  {
    setTitle("Expressions Window");
    setSize(WINDOW_WIDTH,WINDOW_HEIGHT);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    buildPanel();
    add(panel);
    setVisible(true);
  }

   private void buildPanel()
   {
       messageLabel = new JLabel("Please Enter a Number");
       ExTextField = new JTextField(10);
       calcButton = new JButton("Calculate");
       calcButton.addActionListener(new CalcButtonListener());
       panel = new JPanel();
       panel.add(messageLabel);
       panel.add(ExTextField);
       panel.add(calcButton);
   }

   private class CalcButtonListener implements ActionListener
  {
      public void actionPerformed(ActionEvent e)
       {
           String input;
           int num_ent = 0;
           double answerOne;


           input = ExTextField.getText();
           answerOne = Double.parseDouble(input);
           answerOne = num_ent * num_ent;
           JOptionPane.showMessageDialog(null, "Your Answer Is" + answerOne);

        /**
        * here i would like to add another function. input*(n+1.0)/2.0
        *Getting the same number from the user and calculating this aswell. 
        */ 



       }

   }


}

Solution

  • You called the buildpanel method twice, Thats why the String was empty, now it turns the string into a double and prints it out:

         /**
     * 
     *@author ngc5043
     *@version 1.0 
     */
    
    import javax.swing.*;
    import java.awt.event.*;
    
    public class Nicolas_Carabajal_Assignment3 extends JFrame
    {
        private JPanel panel;
        private JLabel messageLabel;
        private JTextField ExTextField;
        private JButton calcButton;
        private final int WINDOW_WIDTH = 310;
        private final int WINDOW_HEIGHT = 100;
    
    
     public static void main(String[] args)
      {
          Nicolas_Carabajal_Assignment3 main = new Nicolas_Carabajal_Assignment3();
    
      }
      public Nicolas_Carabajal_Assignment3()
      {
          buildPanel();
        setTitle("Expressions Window");
        setSize(WINDOW_WIDTH,WINDOW_HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        add(panel);
        setVisible(true);
      }
    
       private void buildPanel()
       {
           messageLabel = new JLabel("Please Enter a Number");
           ExTextField = new JTextField(10);
           calcButton = new JButton("Calculate");
    
           panel = new JPanel();
           panel.add(messageLabel);
           panel.add(ExTextField);
           panel.add(calcButton);
           calcButton.addActionListener(new CalcButtonListener());
       }
    
       private class CalcButtonListener implements ActionListener
      {
          public void actionPerformed(ActionEvent e)
           {
               String input;
               int num_ent = 0;
    
    
               input = ExTextField.getText();
    
               System.out.println(input);
    
               double answerOne = Double.parseDouble(input);
               System.out.println(answerOne);
               answerOne = num_ent * num_ent;
               JOptionPane.showMessageDialog(null, "Your Answer Is" + answerOne);
    
            /**
            * here i would like to add another function. input*(n+1.0)/2.0
            *Getting the same number from the user and calculating this aswell. 
            */ 
    
    
    
           }
    
       }
    
    
    }