Help with this java program?

The program is supposed to read in an integer value and output the number of even, odd, and zero digits in the value.

So here is what I have so far

import java.util.Scanner;

public class EvenOdd

{

public static void main (String [] args)

{

int even, odd, zero, i;

even = 0;

odd = 0;

zero = 0;

i = 0;

Scanner scan = new Scanner(System.in);

System.out.println("Enter an integer value: ");

even = scan.nextInt();

odd = scan.nextInt();

zero = scan.nextInt();

for(i = 0; i < n.length(); i++)

{

switch(n.charAt(i))

{

case '0':

zero ++;

break;

case '2':

case '4':

case '6':

case '8':

even ++;

break;

case '1':

case '3':

case '5':

case '7':

case '9':

}

}

}

}

I know this is not fully right nor complete but I am wondering where to go from here. I get some errors saying the symbol can not be found for the n.length command. Why is that?

1 Answer

Relevance
  • 1 decade ago

    import java.util.Scanner;

    public class EvenOdd

    {

    public static void main (String [] args)

    {

    int even, odd, zero, Number;

    Scanner scan = new Scanner(System.in);

    Number = 1;

    while (Number != 0)

    {

    even = 0;

    odd = 0;

    zero = 0;

    Number = 0;

    System.out.print("Enter an integer value (0 to exit): ");

    try

    {

    Number = scan.nextInt();

    int TempNumber = Number;

    while (TempNumber != 0)

    {

    int Digit = TempNumber % 10;

    TempNumber = TempNumber / 10;

    if (Digit == 0)

    {

    zero ++;

    }

    else if ((Digit & 1) == 0)

    {

    even ++;

    }

    else

    {

    odd++;

    }

    }

    if (Number != 0)

    {

    System.out.println (Number + " has " + odd + " odd digits, " + even + " even digits and " + zero + " zero(s)");

    }

    }

    catch (Exception e)

    {

    System.out.println("Invalid integer");

    }

    System.out.println ();

    System.out.println ();

    }

    }

    }

Still have questions? Get your answers by asking now.