[Solved] How can I convert a string to an int?


Declare z inside your onclick function. Or assign it a value inside your function.

Also nop and cob are editText’s get the data in it, use getText().

Because Integer.parseInt() accepts only a String but not an editText, so you must use getText() to get the String value in it.

x = Integer.parseInt(nop.getText().toString());
y = Integer.parseInt(cob.getText().toString());
 final TextView tv = (TextView) findViewById(R.id.textView);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          int z = x / y;
          tv.setText(z);
        }
    });

Also z is declared globally and assigned nothing. But it needs to be assigned a value when ever you click the button, so that your editText changes every time you click the button.

5

solved How can I convert a string to an int?