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 7599193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:36:07+00:00 2026-05-30T22:36:07+00:00

Using eclipse, first of all. I’m building an Android calculator. I have all my

  • 0

Using eclipse, first of all. I’m building an Android calculator. I have all my code, except there’s a problem. One of my variables is not being used. My total1 variable is basically the counter for the program. When I add two numbers up and press equals, it always prints “0.0” and that’s the value of total1. That means for some reason, it never changed throughout the whole program. I even tested this by using a print statement. I changed the value of total1 and it would print that value I assigned it to. What could be the problem?

public class HelloAndroidActivity extends Activity {
    public EditText display;
    String display1;
    double displayValue;

    // Program is printing out total1, as if it weren't maninpulated in the code
    double total1 = 0.0;
    double total2 = 0.0;

    char theOperator;
    public String buttonText;
    public Button ButtonAdd, ButtonEqual, ButtonMultiply, ButtonDivide, ButtonMinus;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        display = (EditText) findViewById(R.id.editText1);

        // Could it have something to do with this if statement?
        if (display.length() != 0) {
            display1 = display.getText().toString();
            displayValue = Double.parseDouble(display1);
        }
    }

    //Get the operator of the multiply, divide, subtract, add buttons when they are clicked and then do the following
    public void getOperator(String btnText) {
        theOperator = btnText.charAt(0);

        total1 += displayValue;
        display.setText("");
    }

    // Display a string in the box when a number button is clicked
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.bOne:
            display.append("1");        
            break;
        case R.id.bTwo:
            display.append("2");
            break;
        case R.id.bThree:
            display.append("3");
            break;
        case R.id.bFour:
            display.append("4");
            break;
        case R.id.bFive:
            display.append("5");
            break;
        case R.id.bSix:
            display.append("6");
            break;
        case R.id.bSeven:
            display.append("7");
            break;
        case R.id.bEight:
            display.append("8");
            break;
        case R.id.bNine:
            display.append("9");
            break;
        case R.id.bZero:
            display.append("0");
            break;
        case R.id.bPoint:
            display.append(".");
            break;
        case R.id.bClear:
            total2 = 0.0;
            display.setText("");
            break;
        case R.id.bAdd:
            buttonText = "+";
            ButtonAdd = (Button) findViewById(R.id.bAdd);
            ButtonAdd.setText(buttonText);
            getOperator(buttonText);
            break;
        case R.id.bMinus:
            buttonText = "-";
            ButtonMinus = (Button) findViewById(R.id.bMinus);
            ButtonMinus.setText(buttonText);
            getOperator(buttonText);
            break;
        case R.id.bMultiply:
            buttonText = "*";
            ButtonMultiply = (Button) findViewById(R.id.bMultiply);
            ButtonMultiply.setText(buttonText);
            getOperator(buttonText);
            break;
        case R.id.bDivide:
            buttonText = "/";
            ButtonDivide = (Button) findViewById(R.id.bDivide);
            ButtonDivide.setText(buttonText);
            getOperator(buttonText);
            break;

        // Here's the equals button. When I click it it prints out "0.0", the value of total1, instead of adding the total below
        case R.id.bEqual:
            switch (theOperator) {
            case '+':
                total2 = total1 + displayValue;
                break;
            case '-':
                total2 = total1 - displayValue;
                break;
            case '*':
                total2 = total1 * displayValue;
                break;
            case '/':
                total2 = total1 / displayValue;
                break;
            }
            display.setText(Double.toString(total2));
            total1 = 0.0;
            break; 
        }                       
    }
}
  • 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-05-30T22:36:08+00:00Added an answer on May 30, 2026 at 10:36 pm

    You are trying to add displayValue in your get operator method but you have not changed this value. You should be paring this value from the display before adding it to total1 It should be like this:

    public void getOperator(String btnText){
        theOperator = btnText.charAt(0);
    
    
    
        total1+=Double.parseDouble(display.getText().ToString());
        display.setText("");
    }
    

    Or like this if you need to save the current display value:

    public void getOperator(String btnText){
        theOperator = btnText.charAt(0);
    
    
        displayValue = Double.parseDouble(display.getText().ToString());
        total1+=displayValue;
        display.setText("");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After checking out code for the first time from a repository into Eclipse using
I'm developing my first java application using Eclipse. I've recently needed to adjust the
I'm using Eclipse 3.4.1, moving my first steps. When I run my project (a
While using Aptana and Eclipse for the first time in my programming life for
I am trying to create my first GUI application using (Java + Eclipse +
Using Eclipse I want to view the source code for a core Java class
When using Eclipse over X-Windows on a remote shell (X port forwarding), is there
I am using Derby database with Java and Eclipse. I have a table which
I'm developing Scala code using Eclipse, often when I run tests I get this
First of all, I have gone through all the questions here regarding the inclusion

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.