Search code examples
javaswingmethodsjtextfieldcalculator

Calculator produces empty String error for calculation method in Java


OK, I'm writing a calculator in Java and I take an input of a String into my addition method. I use JButtons to write text to a JTextField.

When the user clicks the equals button, I find the relative operation they want to perform (if they click an operator button, I set an int to a specific number (so addition is 1)). I first convert the String to a char array, then check if the chars are numbers or the operator.

I plan to write several methods for all the calculations I am going to make the calculator capable of (saddition, subtraction etc.). I then use the .append method to write the chars that aren't operators into StringBuffers, which I then turn into Strings, and later, doubles. I then perform the calculation, and return the result.

When I try and use the calculator, Eclipse reports a java.lang.NumberFormatException on the line where I try turning the String that held the StringBuffer into a double. The exception is caused by an empty String.

Can anybody explain why this is happening, and provide a solution?

Here is the relevant code:

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


public class Calculator2012 extends JFrame implements ActionListener{

    public static double calculateAdd(String inputString)//this is my addition method
    {
        int s = 0;
        boolean g = true;
        StringBuffer num1 = new StringBuffer();
        StringBuffer num2 = new StringBuffer();
        char[] b = inputString.toCharArray();
        int i = 0;
        if(g==true)
        {
            for(int v = 0; v<b.length; v++)
            {
                if(b[i]!='+')
                {
                    num1.append(b[i]);
                }
                else 
                {
                    g = false;
                    s = ++i;
                    break;
                }
                i++;
            }
        }
        else
        {
            for(int a = 0; a<(b.length-s); a++)
            {
                num2.append(b[s]);
                s++;
            }
        }
        String c1 = num1.toString();
        String c2 = num2.toString();
        double x = Double.parseDouble(c1);
        double y = Double.parseDouble(c2);//this is the error producing line
        double z = x+y;
        return z;
    }

This is my method call:

public void actionPerformed(ActionEvent e)
{
    //omitted irrelevant code

    if(e.getSource()==equals)
    {
        s1 = tf1.getText();
        s2 = " = ";
        s3 = s1+s2;
        tf1.setText(s3);
        if(p==1)//p is my int that detects which operator  to use
        {
            s1 = tf1.getText();
            s2 = Double.toString(calculateAdd(s1));//I call the method here
            s3 = s1+s2;
            tf1.setText(s3);

Solution

  • Since g is true, this part never executes:

        else
        {
            for(int a = 0; a<(b.length-s); a++)
            {
                num2.append(b[s]);
                s++;
            }
        }
    

    Thus num2 is never populated, and you get the exception about trying to parse an empty string.