(kotlin) editText.toString().toInt() isn't work in anroid studio

2.1k Views Asked by At
val editText1 = findViewById<EditText>(R.id.editText1);

if(comNum != editText1.toString().toInt() ){

 View4.text = "오답"
            } else View4.text = "정답"

The installed apk is not working. I think edittext.toString.toInt is wrong.

5

There are 5 best solutions below

1
AudioBubble On BEST ANSWER

Get the String out of EditText: editText1.getText().toString().toInt()

0
IntelliJ Amiya On

Wrong

 editText1.toString().toInt()

It should be

editText1.text.toString().toInt()

FYI

toInt() Parses the string as an Int number and returns the result. if the string is not a valid representation of a number you will receive NumberFormatException .

0
Maitri On

Try this

 val editText1 = findViewById<EditText>(R.id.editText1);

  if(comNum != Integer.parseInt(editText1.text.toString()) ){

     View4.text = "오답"
        } else View4.text = "정답"
0
GianhTran On

Try below code, you can not using editText1.toString().toInt()

  var value: Int

  try {
         value =  editText1.text.toString().toInt();
  } catch (e: NumberFormatException) {
         // value of editText1 is a invalid Integer
  }
  if(comNum != value ){
  View4.text = "오답"
  } else View4.text = "정답"

hope this helps

0
Rajiv Jaiswal On

Use

editText1.text.toString().toInt()