# Problem with my code (Fairly New to Java)

• September 24th, 2013, 08:05 PM
Problem with my code (Fairly New to Java)
Hi java peoples,
I just recently started programming java, and I having a problem with my program. I'm trying to determine the number of even, odd, and zero digits in a number inputted by the user. It counts even and odd digits fine, however, it determines every zero digit to be an even digit. For example, when I enter a number like 140, the program says that there are 2 evens, 1 odd, and 0 zeros, where it should be 1 even, 1 odd, and one zero. Any help would be appreciated.

Code Java:

```public class Project_6 { public static void main (String[] args) { System.out.print ("Enter a number: "); String value = Keyboard.readString(); int evens = 0; int odds = 0; int zeros = 0; int count = 0; int value_int;   while (count < value.length()) { value_int = value.charAt(count); if (value_int % 2 != 0) odds++; else { if (value_int == 0) zeros++; else evens++; } count++; }   System.out.println ("\nEven digits: " + evens + "\nOdd digits: " + odds + "\nZero digits: " + zeros); } }```
• September 24th, 2013, 11:25 PM
GregBrannon
Re: Problem with my code (Fairly New to Java)
If you want to really confuse yourself, add a statement like

System.out.println( "The value is " + value_int );

as the second line of your while statement.

What? Why for 140 are the 'values' printed as 49, 52, and 48? Because those are the char values (or ASCII values) of the characters '1', '4', and '0'. So almost by coincidence, your code half works, because the ASCII values happen to be even for even characters and odd for odd characters. But the ASCII value is also even for the '0' character, so it gets counted as an even.

When you add a test to catch zero, you'll also have to restructure your if/else statements some so that each is at the same level.
• September 25th, 2013, 12:12 AM