Question: I have one edittext and for example I put in12345. I want to check if the first two numbers' sum is >0 then do something. I don't know how to check first two elements' sum.

```
if(pirinfouserid.getText().toString().indexOf(2)>0)
        Toast.makeText(getApplicationContext(), "ok",
                Toast.LENGTH_SHORT).show();
    else
        Toast.makeText(getApplicationContext(), "wrong",
                Toast.LENGTH_SHORT).show();

```

If anyone knows a solution please help me

Thanks

Answer: 
First get first two char from String input from user:

```
String firstTwoDigit = s.substring(0, 
                  Math.min(pirinfouserid.getText().toString().length(), 1));

```

Now get sum of two digits using [Find Sum of Digits in Java Algorithm](http://java67.blogspot.in/2014/06/write-program-to-find-sum-of-digits-in-java.html) :

```
// Parse String to Integer 
int sum = 0;
int digits = Integer.parseInt(firstTwoDigit);
while (digits != 0) {
 int lastdigit = digits % 10;
 sum += lastdigit;
 digits /= 10;
}
if(sum>0){
  // do your work
}else{
  // do your work
}

```

Using this way you will separate digit sum logic from other code which you can later parameterized for finding sum of more digits.

Before using `String.substring` and `Integer.parseInt` also add `null` and `length` check for String
Please find relevant comments inline

```
String input = pirinifouserid.getText().toString();
if(input.length()>2){
    try{
        int num1= Integer.parseInt(input.indexOf(0)); // Handle NumberFormatException
        int num2= Integer.parseInt(input.indexOf(1));// Handle NumberFormatException
        int sum = num1 + nm2;
        if (sum > 0){
            Toast.makeText(getApplicationContext(), "ok",
                Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(getApplicationContext(), "wrong",
                Toast.LENGTH_SHORT).show();
        }
    }catch (NumberFormatException e){ 
        // DO what you think is correct. Probably show an error message 
    }
}

```