0

I'm making a calculator in android, but for some reason it won't let me read from the edit text. this is the code i used to declare the edit text:

final EditText AnswerBox = (EditText) findViewById(R.id.AnswerBox);

This is the code for one of the buttons:

one.setOnClickListener(new View.OnClickListener() {    
        public void onClick (View v)   {   
            AnswerBox.append("1");
        }
    });
}

This is the method that reads the number:

public Double number_reader()

{
    Double num1;
    String s;
    s=AnswerBox.getText().toString();
    num1=Double.valueOf(s);

    return num1;

}

The issue is with the AnswerBox in the number_reader method, it says it can't be resolved..

Carla Dessi
  • 9,086
  • 9
  • 39
  • 53

5 Answers5

1

You probably declare AnswerBox in the onCreate method:

final EditText AnswerBox = (EditText) findViewById(R.id.AnswerBox);

and now AnswerBox is a local variable and it doesn't exist outside the onCreate method so it can't be found in the method number_reader.

To resolve it make a private field in your class :

private final EditText AnserBox;

and then in the onCreate method do this:

AnswerBox = (EditText) findViewById(R.id.AnswerBox);
user
  • 86,916
  • 18
  • 197
  • 190
0

you can try this code..

package com.example.showoff;

import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.text.method.DigitsKeyListener;
import android.view.Menu;
import android.view.View;
import android.widget.Button;


import android.widget.TextView;

public class MainActivity extends Activity {

        TextView edittest1;

        Button one, two, three, four, five, six, seven, eight, nine, zero, add, sub, mul, div, cancel, equal;

        ArrayList<Float> math = new ArrayList<Float>();
        float m1;
        float m2;
        float temp;

        int currentOperation = 0;
        int nextOperation;

        final static int ADD = 1;
        final static int SUBTRACT = 2;
        final static int MULTIPLY =3;
        final static int DIVISION = 4;
        final static int EQUALS = 0;
        final static int CLEAR = 1;
        final static int DONT_CLEAR = 0;
        int clearDisplay = 0;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

            edittest1 = (TextView) findViewById(R.id.edittest1);



            one =(Button) findViewById(R.id.btnNum1Id);
            two =(Button) findViewById(R.id.btnNum2Id);
            three =(Button) findViewById(R.id.btnNum3Id);
            four =(Button) findViewById(R.id.btnNum4Id);
            five =(Button) findViewById(R.id.btnNum5Id);
            six =(Button) findViewById(R.id.btnNum6Id);
            seven =(Button) findViewById(R.id.btnNum7Id);
            eight =(Button) findViewById(R.id.btnNum8Id);
            nine =(Button) findViewById(R.id.btnNum9Id);
            zero =(Button) findViewById(R.id.btnNum0Id);
            add =(Button) findViewById(R.id.btnNumAddId);
            sub =(Button) findViewById(R.id.btnNumSubId);
            mul =(Button) findViewById(R.id.btnNumMulId);
            div =(Button) findViewById(R.id.btnNumDivId);
            cancel =(Button) findViewById(R.id.btnNumClearId);
            equal =(Button) findViewById(R.id.btnNumEqualId);

            edittest1.setKeyListener(DigitsKeyListener.getInstance(true,true));

            registerListeners();


    }

    private void registerListeners() {
        // TODO Auto-generated method stub

        one.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (clearDisplay == CLEAR) {
                    edittest1.setText("");
                }
                clearDisplay = DONT_CLEAR;
               edittest1.append("1");

            }
        });

        two.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (clearDisplay == CLEAR) {
                    edittest1.setText("");
                }
                clearDisplay = DONT_CLEAR;
                edittest1.append("2");

            }
        });

        three.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (clearDisplay == CLEAR) {
                   edittest1.setText("");
                }
                clearDisplay = DONT_CLEAR;
                edittest1.append("3");

            }
        });
        four.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (clearDisplay == CLEAR) {
                  edittest1.setText("");
                }
                clearDisplay = DONT_CLEAR;
                edittest1.append("4");

            }
        });

        five.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (clearDisplay == CLEAR) {
                   edittest1.setText("");
                }
                clearDisplay = DONT_CLEAR;
                edittest1.append("5");

            }
        });

        six.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (clearDisplay == CLEAR) {
                   edittest1.setText("");
                }
                clearDisplay = DONT_CLEAR;
                edittest1.append("6");

            }
        });
        seven.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (clearDisplay == CLEAR) {
                    edittest1.setText("");
                }
                clearDisplay = DONT_CLEAR;
                edittest1.append("7");

            }
        });

        eight.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (clearDisplay == CLEAR) {
                    edittest1.setText("");
                }
                clearDisplay = DONT_CLEAR;
                edittest1.append("8");

            }
        });

        nine.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (clearDisplay == CLEAR) {
                    edittest1.setText("");
                }
                clearDisplay = DONT_CLEAR;
                edittest1.append("9");

            }
        });
        zero.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clearDisplay == CLEAR) {
                    edittest1.setText("");
                }
                clearDisplay = DONT_CLEAR;
                edittest1.append("0");

            }
        });
        add.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {


                calcLogic(ADD);
                }
            }
        );

        sub.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                calcLogic(SUBTRACT);
            }               
        });
        mul.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                calcLogic(MULTIPLY);

            }
        });
        div.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                calcLogic(DIVISION);                    
            }
        });
        equal.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                calcLogic(EQUALS);

            }
        });
        cancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                edittest1.setText("0");
                m1 = 0;
                m2 = 0;
                math.removeAll(math);
                currentOperation = 0;
                nextOperation = 0;    

            }
        });


}
    private void calcLogic(int operator){
         math.add(Float.parseFloat(edittest1.getText().toString()));

            if (operator != EQUALS) {
                nextOperation = operator;
            }
            else if  (operator == EQUALS){
                nextOperation = 0;
                //operator=' ';
            }



            switch (currentOperation) {
            case ADD:               
                m1 = math.get(0);
                m2 = math.get(1);

                math.removeAll(math);

                math.add(m1 + m2);


                edittest1.setText(String.format("%.3f", math.get(0)));



                break;
            case SUBTRACT:
                m1 = math.get(0);
                m2 = math.get(1);

                math.removeAll(math);

                math.add(m1 - m2);

                edittest1.setText(String.format("%.3f", math.get(0)));
                break;
            case MULTIPLY:
                m1 = math.get(0);
                m2 = math.get(1);

                math.removeAll(math);

                math.add(m1 * m2);

                edittest1.setText(String.format("%.3f", math.get(0)));
                break;
            case DIVISION:
                m1 = math.get(0);
                m2 = math.get(1);

                math.removeAll(math);

                math.add(m1 / m2);

                edittest1.setText(String.format("%.3f", math.get(0)));
                break;
            }

            clearDisplay = CLEAR;
            currentOperation = nextOperation;
            if (operator == EQUALS) {
                m1 = 0;
                m2 = 0;
                math.removeAll(math);
            }

        }
         @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}
laalto
  • 150,114
  • 66
  • 286
  • 303
tamil
  • 31
  • 1
0

You declared it final and created it empty. Doubt you can add to it after that.

Barak
  • 16,318
  • 9
  • 52
  • 84
0

I think you need to use Double.parseDouble(s) instead. If you need to, you could refer to an answer I gave here

Community
  • 1
  • 1
Jon
  • 3,174
  • 11
  • 39
  • 57
0

Remove the final keyword and add the EditText in your class fields (declare outside of all methods, but in the Activity). After that it'll be fine to read and write to the EditText. Also for a side note, it's not a good practice to have variable names with capital first letter, this is for a class or interface. Try using a lower camelCase for this.

0xC0DED00D
  • 19,522
  • 20
  • 117
  • 184