ricky
ricky

Reputation: 135

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){}

Upvotes: 1

Views: 89

Answers (3)

h4ck3d
h4ck3d

Reputation: 6364

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.

Upvotes: 2

Artem Zelinskiy
Artem Zelinskiy

Reputation: 2210

The problem is that parser parses string untill first non-number, whitch you have " 3". So you should call

inputNum2 =  Double.parseDouble(val[1].trim());

Upvotes: 0

Jacek Pietal
Jacek Pietal

Reputation: 2019

inputNum2 = Double.parseDouble(val[1]); ?

Upvotes: 2

Related Questions