Jason
Jason

Reputation: 1

Why for some values im getting an error but for others im not?

I'm trying to create a method which takes a number inputted by the user, tests whether each digit in the number is odd, then returns true if all are odd and false if all are even. Heres the code.

    public static boolean allDigitsOdd(int num){

    int digits[] = new int[10];
    int numDigits = 0;
    String numTemp = Integer.toString(num);

    while(num > 1){
        num = num/10;
        numDigits++;
    }
    numDigits++;

    for(int i = 0; i < numDigits; i++){
        digits[i] = numTemp.charAt(i) - '0';
        System.out.println(digits[i]);
    }

    for(int i = 0; i < numDigits; i++){
        if(digits[i] % 2 == 0){
            return(false);
        }
        if(i == numDigits){
            return(true);
        }
    }
    return(true);
}

When I enter '1234' or '1357', it works great and returns the right boolean but when I type almost anything else, it gives me a 'String index out of range' error at

    digits[1] = numTemp.charAt(i) - '0';

Upvotes: 0

Views: 79

Answers (3)

ultimate
ultimate

Reputation: 723

You could even put all the stuff into a simple loop:

public static boolean allDigitsOdd(int num)
{
    while(num != 0)
    {
        if(num % 2 == 0)
            return false; // last digit is even
        num /= 10; // cut of last digit and repeat
    }
    return true; // all digits were odd
}

This will even work for negative numbers!

Upvotes: 0

Davio
Davio

Reputation: 4737

I would solve this another way.

Instead of turning your number into a String (which could give you scientific notation values and/or thousands delimiters, etc.) chop off the last digit and divide by 10 to give you a new number, like so (with recursion!):

public static boolean allDigitsOdd(int num){

    if (num < 0) {
        throw new IllegalArgumentException("num must be 0 or positive");
    }

    if (num < 10) { // for 0-9, this is easy 
        return (num % 2 != 0); // if it isn't even, it's odd
    } else { // here comes the fun part
       int lastDigit = num % 10; // % is the remainder operation, not the modulo operation
       int newNum = num / 10; // because this is how ints work, this chops off the last digit
       return (lastDigit % 2 != 0) && allDigitsOdd(newNum); // recursion fun!
       // This will only return true if all of the tested digits are odd;
       // If there is just one even digit, the && operator returns false
    }
}

System.out.println(allDigitsOdd(1242)); // false
System.out.println(allDigitsOdd(73335799)); // true

You could do this with binary operators, but that just makes it less readable and not that much faster.

Upvotes: 0

Eran
Eran

Reputation: 393831

Change while(num > 1) to while(num >= 10).

Otherwise, it will work only for numbers that start with 1 (such as 1234 and 1357), since for numbers that start with 2 to 9 (such as 9436534 or 4334), your calculation of numDigits will be too high by one, leading to 'String index out of range'.

It would be even better to forget about numDigits and just use numTemp.length() instead.

Upvotes: 1

Related Questions