I ask for help, I make a simple calculator as the first program, but when I erase the last digit, the program crashes and I also could not add a comma to edit text, when I click on a comma on the numeric keypad, the program crashes.

public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button plus; private Button minus; private Button umnogenie; private Button delenie; private TextView text1; private EditText vvod1; private EditText vvod2; private double a,b; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); plus = (Button) findViewById(R.id.button9); minus= (Button) findViewById(R.id.button10); delenie= (Button) findViewById(R.id.button11); umnogenie= (Button) findViewById(R.id.button12); text1= (TextView) findViewById(R.id.textView); vvod1= (EditText) findViewById(R.id.editText); vvod2= (EditText) findViewById(R.id.editText2); plus.setOnClickListener(this); minus.setOnClickListener(this); delenie.setOnClickListener(this); umnogenie.setOnClickListener(this); vvod1.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if (s == null) a= 0; else a = Integer.parseInt(s.toString()); } @Override public void afterTextChanged(Editable s) { } }); vvod2.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if (s == null) b= 0; else b = Integer.parseInt(s.toString()); } @Override public void afterTextChanged(Editable s) { } }); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.button9: text1.setText(String.valueOf(a+b)); break; case R.id.button10: text1.setText(String.valueOf(ab)); break; case R.id.button11: text1.setText(String.valueOf(a/b)); break; case R.id.button12: text1.setText(String.valueOf(a*b)); break; } } } 
  • Add the full text of the error to the question. - Sergey Gornostaev
  • Running the program in the smartphone, I enter the numbers. If I want to add a comma, the program simply gives an error and crashes, I do not know how to fix it. And another thing is that the same situation - I enter numbers and enter what I didn’t want, erase, and when I erased the last digit, the program again gives an error and closes - Dmitry Khevstun
  • Either run the program in the emulator and then get the error in the IDE console, or use logcat and extract the error from the system log of the smartphone. The reason for the departure is written in error. - Sergey Gornostaev
  • The problem is that the program code works without errors, but I do not know how to add or edit these nuances - Dmitry Khevstun
  • 2
    Pay attention to this and similar lines: a = Integer.parseInt (s.toString ()). What happens if you type a comma? - Enikeyschik

1 answer 1

 a = Integer.parseInt(s.toString()); 

Why parse int if you have a Double ?