Search code examples
androidstring-parsing

how to parse a two doubles from editText?


I develop a simple calculator, and I have a problem. When I input the values ​​for the calculations, the first digit is parsed only.For example, if you enter 5 + 3, then the result will be 5.How to solve this problem? Thank's for any answers.

My code:

String[] val = mInputVal.getText().toString().split("\\+");
    try{
        inputNum1 =  Double.parseDouble(val[0]);
        inputNum2 =  Double.parseDouble(strVal);
    }catch (NumberFormatException e){}

Solution

  • For second index just do this : inputNum2 = Double.parseDouble(val[1]); Or make sure strVal = val[1]. And make sure that the string gets split properly. What is happening is that , there is just 1 string in val , due to which val[1] gives you ArrayIndexOutOfBoundException. Hope that helps.