简体   繁体   中英

how do I resolve backspace error in android (java)

I am making a calculator application in java. I added backspace functionality and it displays fine on screen but when the calculation is done it displays some random value .

Example :-

input-1 : 963, input-2 : 78 but i remove 8 by pressing backspace.
operation: input-1 + input-2.
answer: 14.

input-1 : 956, input-2 : 48 but i remove 8 by pressing backspace.
operation: input-1 + input-2.
answer: 8.

also if I use subtraction operation(with backspace) then it always returns 0.

this is my code for backspace

   private String input, input2 ;
   private Double input_number;

   boolean Addition, Subtract, Multiplication, Division, mRemainder, decimal, add_sub;

            num_backspace.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String sample_input = (num_input.getText() + "");
                input = sample_input.substring(0, sample_input.length() - 1);
                num_input.setText(input);

                if (input.contains(".")){
                    decimal = true;
                }
                else{
                    decimal = false;
                }
            }
        });

And this is my code for equals

            num_equals.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Addition || Subtract || Multiplication || Division || mRemainder) {
                    input2 = (num_input.getText() + "");
                }

                if (Addition) {

                    float final_input = Float.parseFloat(input);
                    float final_input2 = Float.parseFloat(input2);
                    num_input.setText(new Float(final_input + final_input2 + "").toString().replaceAll("\\.?0*$", ""));
                    num_output.setText(new Float(final_input + final_input2 + "").toString().replaceAll("\\.?0*$", ""));
                    Addition = false;
                }

                if (Subtract) {

                   float final_input = Float.parseFloat(input);
                    float final_input2 = Float.parseFloat(input2);
                    num_input.setText(new Float(final_input - final_input2 + "").toString().replaceAll("\\.?0*$", ""));
                    num_output.setText(new Float(final_input - final_input2 + "").toString().replaceAll("\\.?0*$", ""));
                    Subtract = false;
                }

                if (Multiplication) {

                    float final_input = Float.parseFloat(input);
                    float final_input2 = Float.parseFloat(input2);
                    num_input.setText(new Float(final_input * final_input2 + "").toString().replaceAll("\\.?0*$", ""));
                    num_output.setText(new Float(final_input * final_input2 + "").toString().replaceAll("\\.?0*$", ""));
                    Multiplication = false;
                }

                if (Division) {
                    float final_input = Float.parseFloat(input);
                    float final_input2 = Float.parseFloat(input2);
                    num_input.setText(new Float(final_input / final_input2 + "").toString().replaceAll("\\.?0*$", ""));
                    num_output.setText(new Float(final_input / final_input2 + "").toString().replaceAll("\\.?0*$", ""));
                    Division = false;
                }
                if (mRemainder) {

                    float final_input = Float.parseFloat(input);
                    float final_input2 = Float.parseFloat(input2);
                    num_input.setText(new Float(final_input % final_input2 + "").toString().replaceAll("\\.?0*$", ""));
                    num_output.setText(new Float(final_input % final_input2 + "").toString().replaceAll("\\.?0*$", ""));
                    mRemainder = false;
                }
            }
        });

if you want you can look at the full code here

https://github.com/vendz/Calculator/blob/master/app/src/main/java/com/example/calculator/MainActivity.java

Hey you are resetting the input field on click of backpress , change your backpress code to below and it will work

num_backspace.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String sample_input = (num_input.getText() + "");
                String mInput = sample_input.substring(0, sample_input.length() - 1);
                num_input.setText(mInput);

                if (mInput.contains(".")){
                    decimal = true;
                }
                else{
                    decimal = false;
                }
            }
        });

There are 4 buttons here in general.

  1. 0 , 19 : add digit to num_input
  2. + , assign num_input to input . Clear num_input
  3. <- , input = num_input.substring(0, n-1)
  4. = , assign num_input to input2

The issue is that, backspace can happen before or after an operation is selected. However, backspace always subtract string from num_input . At the same time, num_input could be the source of data for input and input2 (see pt 2 & 4). If I key in this sequence 4 0 + 1 2 <- = . It supposed to be 40+1=. But what actually happen during the <- is it overwrite 1 to input instead of input2 . That is why from your example, the incorrect answer = second number + second number.

input-2: 78, answer = 7 + 7 = 14

input-2: 48,answer = 4 + 4 = 8

Besides, I have some recommendation reading your question. They are not necessary and may not directly related to the issue, just some suggestion in general programming.

  1. Don't store number as String. You do not need to get the numeric value the calculator currently hold by extracting it form the TextView. You have your input_number which hold the true value of the number. Take it as the most accurate source and treat the TextView as a representation only. Doing the other way round may not be a good idea. String manipulation and parsing is not very safe to do (parsing error). Moreover, it sounds an extra step to me.
  2. Addition, Subtract, Multiplication, Division. A good showcase for command pattern. You don't want to write a very long if. It is bad for maintainability. Consider in the future you may add more feature to the calculator, sin, cos, log, power etc. Soon the code will become messy.
  3. Why (num_input.getText() + "") ? getText() return String, you don't need Implicit Conversion
  4. num_period set onclick listener twise.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM