-1

I am making the calculator app.
I tried to make the delete button, but there are errors.
(1) If I press backspace when there is no number, the app closed suddenly.
(2) If I press a new number after deleting the number, the previous deleted number shows up again.

I searched a lot about it but I cannot understand them as a beginner.
I would appreciate it if you can explain it easily.

public class MainActivity extends AppCompatActivity {

    TextView workingsTV;
    TextView resultsTV;

    String workings = "";

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

    private void initTextView()
    {
        workingsTV = (TextView)findViewById(R.id.workingsTextView);
        resultsTV = (TextView)findViewById(R.id.resultTextView);
    }

    private void setWorkings(String givenValue)
    {
        workings = workings + givenValue;
        workingsTV.setText(workings);
    }

    public void equalsOnClick(View view)
    {
        Double result = null;
        ScriptEngine engine = new ScriptEngineManager().getEngineByName("rhino");

        try {
            result = (Double) engine.eval(workings);
            if (result != null)
            {
                int intVal = (int) result.doubleValue();

                if (result == intVal)
                {//Check if it's value is equal to its integer part
                    resultsTV.setText(String.valueOf(intVal));
                }
                else
                {
                    resultsTV.setText(String.valueOf(result));
                }
            }
        }
        catch (ScriptException e) {
            Toast.makeText(this, "Invalid Input", Toast.LENGTH_SHORT).show();
        }

    }

    public void deleteOnClick(View view)
    {

        String del_number = workingsTV.getText().toString();
        workingsTV.setText(del_number.substring(0,del_number.length() - 1));

    }
Ichigo Kurosaki
  • 3,765
  • 8
  • 41
  • 56
cloud
  • 77
  • 6

2 Answers2

1

Problem 1 is because del_number.length() is 0, so del_number.length()-1 is -1 which is an illegal parameter to substring. The easiest way to fix it is to not do anything if the length is 0.

Problem 2 is because you don't reset the variable workings when you delete. It needs to be set to "". Or don't have that variable at all and always use the workingsTV.getText() be the source of truth.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • Thank you for explaining. I've learned programming only for a month that it's hard to code without error. Could you show me how to code it, please? – cloud Dec 29 '20 at 05:50
  • I solved Problem 1 after editing the code like `public void deleteOnClick(View view){if(workingsTV.getText().toString().length() > 1) {String getResultText = workingsTV.getText().toString(); String subString = getResultText.substring(0, getResultText.length() -1); workingsTV.setText(subString); } else { workingsTV.setText(CLEAR_INT_TEXT); } }` – cloud Jan 03 '21 at 03:18
  • But I cannot solve Problem 2. I tried a few different codes and all made errors. – cloud Jan 03 '21 at 03:22
0


I found the solution. I edit the code like this to prevent the app closed suddenly.

    public void deleteOnClick(View view) {
        if(workingsTV.getText().toString().length() >= 1) {
            String getResultText = workingsTV.getText().toString();
            String subString = getResultText.substring(0, getResultText.length() -1);
            workingsTV.setText(subString);

        }
        else
            {
            workingsTV.setText(CLEAR_INT_TEXT);
        }
    }


But still I had a problem that the letter I deleted comes back when I press a new number.
The solution was this.
workings = workings.substring(0, workings.length() -1);
The letter at workings should be deleted as well like workingsTV


So here is full code for delete

    public void deleteOnClick(View view) {
        if(workingsTV.getText().toString().length() >= 1) {
            String getResultText = workingsTV.getText().toString();
            String subString = getResultText.substring(0, getResultText.length() -1);
            workingsTV.setText(subString);

             workings =  workings.substring(0, workings.length() -1);
        }
        else
            {
            workingsTV.setText(CLEAR_INT_TEXT);
        }
    }
cloud
  • 77
  • 6