Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8309273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:03:54+00:00 2026-06-08T19:03:54+00:00

OK, I’m writing a calculator in Java and I take an input of a

  • 0

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);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T19:03:56+00:00Added an answer on June 8, 2026 at 7:03 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.