0

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

  1. 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?

  2. It prints the wrong answer 49 for 56781 when I put return x; at the end of the method.

  3. 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) );       
}
3
  • i believe the problem is stringn.charAt(x) %2 in if statement, you are performing % operator on a character, you should convert it back to integer before performing % Commented Sep 23, 2017 at 4:35
  • Is stringn.charAt(x) a String type or char type? And how to you convert it into int. Will (int)(stringn.charAt(x) ) do the trick? Commented Sep 23, 2017 at 4:40
  • I guess you already got the answer, the return statement is inside an if statement inside a loop, and will only be executed when the condition is true. Commented Sep 23, 2017 at 4:49

1 Answer 1

1

You presumably don't want to return immediately when you find the first odd digit. You also need to convert the result back into an int (to match your return type). You can parse it with Integer.parseInt(String); and you can build that String with a StringBuilder. Also, you could make the method static. Like,

static int countOddDigits(int i) { // <-- camel case, that is camelCase
    StringBuilder sb = new StringBuilder();
    // for each char ch in the String form of i converted to a character array
    for (char ch : String.valueOf(i).toCharArray()) {
        if (Character.digit(ch, 10) % 2 != 0) {
            sb.append(ch);
        }
    }
    return Integer.parseInt(sb.toString());
}

Then, to test it, you can call it like

public static void main(String[] args) {
    int oddDigits = countOddDigits(56781);
    System.out.println(oddDigits);
}

Which outputs

571
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Could you plz explain missing return statement error? I have a return statement in my if condition.
@hyden55Printf - You're returning a value only when if-statement condition is true; if the if-statement doesn't evaluate to true,, no values will be returned by the method - hence the error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.