# Code for EvenCount

• August 21st, 2012, 08:01 AM
Mehwish-S
Code for EvenCount
Create a class called EvenCount that contains a method called count that takes an integer as an argument and returns an integer (this method should not be declared static). The count method should use a recursive approach to find the amount of even digits in the integer passed into the method, and return this number. For example, if the count method was called with the input value 783312 as an argument it should return 2 (there are two even numbers in the input value). Hint: in Java a single digit that is even will produce a result of 0 when the remainder operator (the percent sign) is used to find its remainder when divided by two. For example 2 % 2 and 6 % 2 will return 0, whereas 3 % 2 and 7 % 2 will produce a result of 1.

Code :

package mocktest;

public class EvenCount {

public String count(String x) {

if (x < 10 && x % 2 == 0)
return 1;
else if (x < 10 && % 2 !0)
return 0;
else if ((x >=10 && (x / 10 ) % 2 == 0)
return 1 + count(x/10) + 1;
else
return count (x/10);
}
public static void main (String args[]); {

EvenCount c = new EvenCount ();
System.out.println(x.count(73221));
}
}

Why do i have so many errors?
• August 21st, 2012, 08:53 AM
Norm
Re: Code for EvenCount
You forgot to post the full text of the error messages. Please post them if you need help.
• August 21st, 2012, 12:26 PM
copeg
Re: Code for EvenCount
Mehwish-S, read the forum rules. Do NOT double post. I have moved and locked your other post.
• August 21st, 2012, 05:45 PM
Samaras
Re: Code for EvenCount
Is it ok to compare a string to a number?I do not think so
• August 21st, 2012, 05:51 PM
Norm
Re: Code for EvenCount
By number do you mean an int or a double?
To compare an int or double to a String, you need to convert one to the same data type as the other.

In some cases the compiler using autoboxing will convert a primitive to an object.
• August 21st, 2012, 06:55 PM
Samaras
Re: Code for EvenCount
I agree about autoboxing but look at this

Code java:

public String count(String x) {

if (x < 10 && x % 2 == 0)
...

is this acceptable?
• August 21st, 2012, 07:06 PM
pbrockway2
Re: Code for EvenCount
The assignment instructs were pretty explicit: the required should take an int argument and return the even (base 10) digit count as an int.

So forget String. The recursion requirement is weird enough (in Java) without being autoboxed about the ears to boot.
• August 21st, 2012, 07:11 PM
pbrockway2
Re: Code for EvenCount
D@mn, I can' tedit posts on my phone. Instruct should be instructions and the required method must take and return an int.

Do think about the recursion requirement before you begin to code. You are looking for a recipe that will get you part of the answer and call itself to get the rest.
• August 22nd, 2012, 09:02 AM
Darryl.Burke
Re: Code for EvenCount
This thread has been cross posted here: