7 Replies Latest reply: Jun 16, 2009 1:49 PM by 684401 RSS

    Find the odd,even,zero digits of an integer

    843785
      Hi,
      Here i have an assignment I have to do. It is stated as a comment in the code. Every time I run the program, it thinks that every digit of the integer is odd because when I use String.indexOf on a number, it returns -1.
      Can somebody help me edit this code?
      Thanks
      /*
      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
      */
       
      
      import java.util.Scanner;
      
      public class OddEvenZero
      {
           public static void main (String[] args)
           {
                int odds = 0;
                int evens = 0;
                int zeros = 0;
                Scanner scan = new Scanner (System.in);
                System.out.println("Enter an Integer");
                int num = scan.nextInt();
                
                String numS = Integer.toString(num);
                int digits = numS.length();
                
                for (int i = 0; i < digits; i++)
                {
                     if (numS.indexOf(i) == 0)
                     zeros++;
                     else if
                     (numS.indexOf(i) % 2 == 0)
                     evens++;
                     else
                     odds++;
                }
                
                System.out.println("The number of odd digits = " + odds);
                System.out.println("The number of even digits = " + evens);
                System.out.println("The number of zero digits = " + zeros);
           }
      }