Determining the difference between odd and even numbers I have code that converts each character of a String to an int and returns the difference between odd and even numbers. Can this code be simplified?

int compareSumOfDigits(String N) {
int e=0,o=0;
for (int i =0;i&lt;N.length();i++){
    int t = Character.getNumericValue(N.charAt(i));
    if(t%2==0)
    e+=t;
else
    o+=t;

}
      return  o-e ;
}
Combining some of the existing answers, you can do

s.chars()                          // get the char stream
  .map(Character::getNumericValue) // convert to ints
  .map(n -&gt; n%2==0 ? -n : n)       // negate the even ones
  .sum()                           // sum it all up


This will give you the sum of the odds and the negative evens, which is the same as the sum of the odds minus the sum of the evens.

edit

In response to @kai, for absolute max readability, I'd probably do (pseudocode)

List&lt;int&gt; ints = s.chars().map(Character::getNumericValue)
Map&lt;Boolean, List&lt;int&gt;&gt; intsEven = ints.partitioningBy(n -&gt; n%2==0)
return intsEven.get(false).sum() - intsEven.get(true).sum()


or with isEven from the other answers and not defined here

List&lt;int&gt; ints = s.chars().map(Character::getNumericValue)
List&lt;int&gt; evens = ints.filter(isEven)
List&lt;int&gt; odds = ints.filter(not(isEven))
return odds.sum() - evens.sum()