Good day! The task is to get the numeric value from the EditText and return it. I doubt if I wrote the verification of a numeric value correctly if EditText is empty. Here is the method:

private int getEditText(EditText editText){//Возвращает значение из EditText в формате int int tmp; String text = editText.getText().toString(); if (text.isEmpty()) tmp = 0; else tmp = Integer.parseInt(text); return tmp; 

the layout of the EditText is the value android: inputType = "number"

    1 answer 1

    Not really ideal :) Already ready text taken from EditText should be sent to your function

     //где-то getEditText(editText.getText().toString(); //функция int getEditText(String s); //если текст есть if(!s.equals("")){ //возвращаем его в формате int return Integer.parseInt(s); } //иначе else{ //придумайте сами } 

    I apologize for the term "function", I meant "method"

    UPD If you need a method with an EditText parameter on input, here's

     int getEditText(EditText editText){ String text = editText.getText().toString(); if(!text.equals("")) return Integer.parseInt(text); else //если пуст } 

    But still, I advise the first method, since passing EditText is somehow a crutch.

    • Instead of nullpointerexception it will turn out when using equals. If EditText is empty, then its value will be null. And it turns out that we will compare null c "". - Alexander
    • No, if EditText empty, then the text is "" . Equals do not like it, try checking length to 0 - Flippy