# Code for EvenCount

• 08-21-2012, 03:14 PM
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.

Java code:
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? :/
& how can i get rid of them?
• 08-21-2012, 04:40 PM
jhuber151
Re: Code for EvenCount
Ok so with this one your first error is the semi-colon next to you main class parameters. Next your directions say use an int as the parameters and you are using a String. So you will have to change the count methods parameters the be an int. Also with the return type, you declare it as a String also, so that would need changing. Lastly it would be c.count not x.count for the method call.
• 08-21-2012, 04:52 PM
Fubarable
Re: Code for EvenCount
• 08-21-2012, 04:53 PM
DarrylBurke
Re: Code for EvenCount
Moved from Advanced Java. Please confine beginner questions to the New to Java section.

db
• 08-21-2012, 06:09 PM
Mehwish-S
Re: Code for EvenCount
Quote:

Originally Posted by Fubarable

Sorry, im new so dont really know much on how to use this and how to provide links
• 08-22-2012, 03:26 PM
Mehwish-S
Re: Code for EvenCount
Quote:

Originally Posted by jhuber151
Ok so with this one your first error is the semi-colon next to you main class parameters. Next your directions say use an int as the parameters and you are using a String. So you will have to change the count methods parameters the be an int. Also with the return type, you declare it as a String also, so that would need changing. Lastly it would be c.count not x.count for the method call.

Im confused.. i dont know which semi-colon to take off. I tried all but still an error. and which return type is declared as String?
• 08-22-2012, 03:30 PM
Norm
Re: Code for EvenCount
Please post the full text of the compiler's error messages.
• 08-22-2012, 03:41 PM
Mehwish-S
Re: Code for EvenCount
Quote:

Originally Posted by Norm
Please post the full text of the compiler's error messages.

I cant run it because it says it doesnt have a main method :/
• 08-22-2012, 03:52 PM
jhuber151
Re: Code for EvenCount
The semi-colon i was talking about was :
Code:

` public static void main (String args[]);`
it should look like
Code:

` public static void main (String args[])`
Also the return type i was referring to is
Code:

`public String count(String x) {`
The String after public is your return type and the String x in the parenthesis is the parameters.
• 08-22-2012, 03:53 PM
Norm
Re: Code for EvenCount
You do not need a main() method to compile a program.

Please compile the program and copy the full text of the error messages here.
• 08-22-2012, 04:11 PM
Mehwish-S
Re: Code for EvenCount
Quote:

Originally Posted by Norm
You do not need a main() method to compile a program.

Please compile the program and copy the full text of the error messages here.

I am using netbeans therefore i do need main() method
• 08-22-2012, 04:12 PM
Mehwish-S
Re: Code for EvenCount
Quote:

Originally Posted by jhuber151
The semi-colon i was talking about was :
Code:

` public static void main (String args[]);`
it should look like
Code:

` public static void main (String args[])`
Also the return type i was referring to is
Code:

`public String count(String x) {`
The String after public is your return type and the String x in the parenthesis is the parameters.

Code:

```        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(c.count(73221)); }```
i have an error on each of these lines.. fustrating :/ can't get rid of them
• 08-22-2012, 04:14 PM
Norm
Re: Code for EvenCount
Quote:

i have an error
Please post the full text of the compiler's error messages.
• 08-22-2012, 04:24 PM
Mehwish-S
Re: Code for EvenCount
Quote:

Originally Posted by Norm
Please post the full text of the compiler's error messages.

I cannot run it because i am using netbeans. Can you please just paste it on netbeans and see the syntax error. There are to many to describe. thanks!
• 08-22-2012, 05:06 PM
Norm
Re: Code for EvenCount
Sorry, I don't use an IDE.
Try using the javac command with the source in a command prompt window.
• 08-22-2012, 05:09 PM
jhuber151
Re: Code for EvenCount
Quote:

Originally Posted by jhuber151
The semi-colon i was talking about was :
Code:

` public static void main (String args[]);`
it should look like
Code:

` public static void main (String args[])`
Also the return type i was referring to is
Code:

`public String count(String x) {`
The String after public is your return type and the String x in the parenthesis is the parameters.

Quote:

Originally Posted by jhuber151
Ok so with this one your first error is the semi-colon next to you main class parameters. Next your directions say use an int as the parameters and you are using a String. So you will have to change the count methods parameters the be an int. Also with the return type, you declare it as a String also, so that would need changing. Lastly it would be c.count not x.count for the method call.

Quote:

Originally Posted by Mehwish-S
Code:

```        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(c.count(73221)); }```
i have an error on each of these lines.. fustrating :/ can't get rid of them

In my previous posts I gave you hints to solve each of those syntax errors
• 08-22-2012, 05:33 PM
Mehwish-S
Re: Code for EvenCount
Quote:

Originally Posted by jhuber151
In my previous posts I gave you hints to solve each of those syntax errors

Thanks! i got most of the errors sorted but what does it mean by class,interface or enum expected?
Lastly, on this line
Code:

` else if (c < 10 && % 2 !0)`
it says illegal start of expression
')' and ';' expected
not a statement

what does this mean?
• 08-22-2012, 09:45 PM
jhuber151
Re: Code for EvenCount
For this line you technically what you are doing is first seeing if c is less then 10, but then after that the second half of the line is not complete
Code:

`((x >=10) && (x % 2 != 0)) //<-- This says if x is greater then 10 AND x mod 2 is not equal to  0`
When using && or || you need to have the x on both sides to allow it to perform correctly.