Hello there. Here is my task which I am stuck on:
"Design and implement an application that determines and prints the number of odd, even and zero digits in an integer value read from the keyboard."
I have made variable pos the place at which the scanner is scanning a particular digit and made variable length equal the position of the 'farthest right' digit so that the scanning moves from right to left, checking each value of pos. It compiles and runs but the results I get are wrong. I would much appreciate any advice. Even if that means not using a for loop and starting fresh.
Thanks
//numbers.java import java.util.Scanner; public class numbers { public static void main (String[] args) { int num, pos, odd=0, even=0, zero=0, length = 0; Scanner scan = new Scanner (System.in); System.out.println ("Please enter an integer: "); num = scan.nextInt(); //.length() etc length = String.valueOf(num).trim().length(); for (pos = length - 1; pos >= 0; pos--) { if (pos == 0) zero++; if (pos % 2 == 0) even++; if (pos % 2 != 0) odd++; } System.out.println("zeros: " + zero); System.out.println("evens: " + even); System.out.println("odds: " + odd); } }