0

What I want is that by default C is 0 but when the user adds 2 then it should display 2 and then when the user adds 4 it should be 6 but instead it displays 4 Any help would be appreciated!

    public void addCash(View view) {
    //Intent intent = new Intent(this, Main.class);

    EditText val1 = (EditText) findViewById(R.id.num);
    int b = 0;
    int a = 0;
    int c = 0;
    int d = 0;

    a = Integer.parseInt(val1.getText().toString());

     if (c == 0) {
         c = a + b;
     }
     else {
         c = c + a;
     }

    TextView result = (TextView) findViewById(R.id.outPut);
    result.setText(""+c);

    //startActivity(intent);
}
9
  • You always initialize c to 0. Commented Sep 25, 2017 at 4:06
  • You need c to be an instance variable in an object, not a local variable in a method. If it's a local variable in a method, you can't retain the value between method calls. The value goes away as soon as addCash is done. Commented Sep 25, 2017 at 4:07
  • 1
    post your complete class Commented Sep 25, 2017 at 4:07
  • Also, what's the point of c = a + b if b can never be anything other than 0? You could just say c = a. But since you only do this if c == 0, saying c = a is exactly the same as c = c + a. In other words, this whole if statement is redundant. Also, what is d there for? Commented Sep 25, 2017 at 4:09
  • How to make C an Instance Variable Commented Sep 25, 2017 at 5:08

4 Answers 4

1

variable c is local to this function and every time function is called it initialize c = 0 .

So every time only if condition will run.

else condition will never executed.

you can declare c as global ....

Sign up to request clarification or add additional context in comments.

2 Comments

How to declare C as global int?
for global declaration you have to declare c when your class start.. Eg : class Test { private int c; Test(){c=0;} fun(){ //use c } }
0

Initialize your variable c with the value of the textview like this:

TextView result = (TextView) findViewById(R.id.outPut);
int c = Integer.parseInt(val1.getText().toString());

Edit: if the TextView by default is not set to 0 then you should consider doing an additional check.

2 Comments

How to set it to default 0?
@MusaUsman In the Activity's onCreate method add this: TextView result = (TextView) findViewById(R.id.outPut); result.setText("0");
0

Initialize "c" as a global member variable of your activity.Because instance of this will remain throughout the activity whereas if you initialize it as local vairable every time when you call method new variable instance is created

This as the global instance inside your activity

private int c = 0;

These "a",b","d" be initialzed again and again when call addcash method

public void addCash(View view) {
//Intent intent = new Intent(this, Main.class);

EditText val1 = (EditText) findViewById(R.id.num);

int b = 0;
int a = 0;
int d = 0;

a = Integer.parseInt(val1.getText().toString());

 if (c == 0) {
     c = a + b;
 }
 else {
     c = c + a;
 }

TextView result = (TextView) findViewById(R.id.outPut);
result.setText(""+c);

Also replace below line of code with c = c + a as "b" will be always zero according to your code

if (c == 0) 
{
   c = a + b;
}

For more information check this link

http://www.cafeaulait.org/course/week3/11.html

Comments

0

As the other answers already stated, you are using local variables, which are initialized at each method call.

class SomeActivity {

    private int c = 3; // Instance variable being initialized to 3. When you
                       // leave out the "= 3" part, then it is initialized
                       // to 0.

    public void addCash(View v) {
        // Now if you add something to c, it will persist between multiple
        // method calls
        EditText inputField = (EditText) findViewById(R.id.num);
        EditText outputField = (TextView) findViewById(R.id.outPut);
        c += Integer.parseInt(inputField.getText().toString());
        outputField.setText(String.valueOf(c));
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.