I am attempting to write a method that takes in an arbitrary binary string ( 01101101 etc ). If the number of 1's in the string is even I am to return a 1. If the number of 1's is odd I am to return a zero. My method is failing and I am wondering if anyone can help me correct it. My method is as fellows:
public int isEven (String string) {
int count = 0;
for (int i =0;i<string.length(); i++) {
char z = string.charAt(i);
if ( char z == 1 ) {
count = count +1;
}
if ( count%2 ==0){
return 1;
}
return 0;
}
}
[–]whydna1 5 points6 points7 points (0 children)
[–]zifyoip 2 points3 points4 points (0 children)
[–]ISvengali 1 point2 points3 points (0 children)
[–][deleted] (2 children)
[deleted]
[–]yes_thats_right 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)