I am suppose to find odd numbers in an integer for HW. So countOddigtis(56781) should return( NOT PRINT ) 5 7 1. My approach was to convert the integer into a string and use that to return. Problems I am having are
Missing return statement error, even though I have a return statement in the if statement. Can someone explain what this error means and how to get past it?
It prints the wrong answer 49 for 56781 when I put return x; at the end of the method.
Can Java solve stringn.charAt(x) % 2 != 0 considering I am might(NOT SURE) be comparing a string or char with an int?
P.S keep it simple, I don't much Java, I just started.
int countOddigits( int n )
{
int x = 0;
String stringn = Integer.toString(n);
while ( x <= stringn.length() - 1 )
{
if ( stringn.charAt(x) % 2 != 0 )
{
return stringn.charAt(x);
}
x++;
}
}
public void run()
{
System.out.printf("Odd number is %d\n: ", countOddigits(567981) );
}
stringn.charAt(x) %2inifstatement, you are performing%operator on a character, you should convert it back to integer before performing%returnstatement is inside anifstatement inside a loop, and will only be executed when the condition is true.