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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:06:51+00:00 2026-05-30T14:06:51+00:00

I’m programming a Android Calculator. I have all my code, but when use the

  • 0

I’m programming a Android Calculator. I have all my code, but when use the calculator, it always prints “0.0”, which is the original I set for total1 and total2. So, my assumption is that the methods are not seeing those two variables. So I assume I have to set them as static variables so that the rest of my code can use them and change them. I may be setting up the static variable wrong. But after setting total1 and total2 as static variables, I still have the same problem. My other guess about why my program isn’t working is my displayValue variable. In my if statement, it says that the local variable displayValue is not used. I would appreciate any help.

package rechee.cool;

import android.app.Activity;

import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;




public class HelloAndroidActivity extends Activity {


/** Called when the activity is first created. */
public EditText display;
  String display1;
 double displayValue;

// I want to be able to use the following two variables everywhere in the code.
static double total1=0.0;
static 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);




    if(display.length()!=0){
    String display1= display.getText().toString();
    // Says local variable displayValue has not been used. May be my problem?
    double displayValue= Double.parseDouble(display1);
}
}








    public void getOperator(String btnText){
        theOperator = btnText.charAt(0);



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







        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;  
                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-30T14:06:53+00:00Added an answer on May 30, 2026 at 2:06 pm

    You’re re-declaring variables local only to the scope of onCreate. Try

    display1 = display.getText().toString();
    //   v ^ these now refer to the members of class HelloAndroidActivity
    displayValue = Double.parseDouble(display1);
    

    Suggested reading

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a text area in my form which accepts all possible characters from
I am trying to understand how to use SyndicationItem to display feed which is
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small

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.