1. Member
Join Date
Aug 2012
Posts
12
Rep Power
0

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:
Java 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?

2. Member
Join Date
Jul 2012
Posts
55
Rep Power
0

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.

3. Re: Code for EvenCount

Please don't post the same question on multiple forums without notifying us. This risks our wasting our time answering questions that have already been answered elsewhere. All we ask is that you provide links to your other questions, and that's not asking too much. Please help us out here. Doing this will also help prevent you from going on user's do-not-help lists.

4. Re: Code for EvenCount

Moved from Advanced Java. Please confine beginner questions to the New to Java section.

db

5. Member
Join Date
Aug 2012
Posts
12
Rep Power
0

Re: Code for EvenCount

Originally Posted by Fubarable
Please don't post the same question on multiple forums without notifying us. This risks our wasting our time answering questions that have already been answered elsewhere. All we ask is that you provide links to your other questions, and that's not asking too much. Please help us out here. Doing this will also help prevent you from going on user's do-not-help lists.
Sorry, im new so dont really know much on how to use this and how to provide links

6. Member
Join Date
Aug 2012
Posts
12
Rep Power
0

Re: Code for EvenCount

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?

7. Re: Code for EvenCount

Please post the full text of the compiler's error messages.

8. Member
Join Date
Aug 2012
Posts
12
Rep Power
0

Re: Code for EvenCount

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 :/

9. Member
Join Date
Jul 2012
Posts
55
Rep Power
0

Re: Code for EvenCount

The semi-colon i was talking about was :
Java Code:
` public static void main (String args[]);`
it should look like
Java Code:
` public static void main (String args[])`
Also the return type i was referring to is
Java Code:
`public String count(String x) {`
The String after public is your return type and the String x in the parenthesis is the parameters.

10. 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.

11. Member
Join Date
Aug 2012
Posts
12
Rep Power
0

Re: Code for EvenCount

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

12. Member
Join Date
Aug 2012
Posts
12
Rep Power
0

Re: Code for EvenCount

Originally Posted by jhuber151
The semi-colon i was talking about was :
Java Code:
` public static void main (String args[]);`
it should look like
Java Code:
` public static void main (String args[])`
Also the return type i was referring to is
Java Code:
`public String count(String x) {`
The String after public is your return type and the String x in the parenthesis is the parameters.
Java 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

13. Re: Code for EvenCount

i have an error
Please post the full text of the compiler's error messages.

14. Member
Join Date
Aug 2012
Posts
12
Rep Power
0

Re: Code for EvenCount

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!

15. Re: Code for EvenCount

Sorry, I don't use an IDE.
Try using the javac command with the source in a command prompt window.

16. Member
Join Date
Jul 2012
Posts
55
Rep Power
0

Re: Code for EvenCount

Originally Posted by jhuber151
The semi-colon i was talking about was :
Java Code:
` public static void main (String args[]);`
it should look like
Java Code:
` public static void main (String args[])`
Also the return type i was referring to is
Java Code:
`public String count(String x) {`
The String after public is your return type and the String x in the parenthesis is the parameters.
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.
Originally Posted by Mehwish-S
Java 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

17. Member
Join Date
Aug 2012
Posts
12
Rep Power
0

Re: Code for EvenCount

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
Java Code:
` else if (c < 10 && % 2 !0)`
it says illegal start of expression
')' and ';' expected
not a statement

what does this mean?

18. Member
Join Date
Jul 2012
Posts
55
Rep Power
0

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
Java 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.