3

I have an array with integers inside it. I want to iterate through them to check if its divisible by 2, 3, 5. At the moment my code only run once.

So say if I have 6 in the list. It will only return "6 is divisible by 2" where it should be "6 is divisible by 2 and 3"

So how do I make the code more elegant. Is there a way to write the code without having to define like if (number % 2 == 0) && (number % 3 == 0)... or it has to be done that way? defining each of the condition each time.

this is my code

public class checkingDivisibility {
    public static void main(String[] args) {
        int list[] = {1, 2, 3, 6, 8, 10, 12, 14, 15, 17, 19, 21};
        for (int x : list) {
            if (x % 2 == 0) {
                System.out.println(x + "div by 2 possible");
            } else if (x % 3 == 0) {
                System.out.println(x + "div by 3 possible");
            } else if (x % 5 == 0) {
                System.out.println(x + "div by 5 possible");
            }
            }
        }
    }
3
  • 3
    Remove the else clauses so the ifs evaluate independently. Commented May 15, 2017 at 9:49
  • Handy tip: First, make the code work. Then, and only then, worry about making it "elegant". Commented May 15, 2017 at 10:47
  • thanks for the input @KevinAnderson noted :) Commented May 15, 2017 at 11:11

4 Answers 4

7

You have an else if after the if, which means that the next if condition is evaluated only if the first one was false. This is not what you want.

What you want is, that each condition should be checked. Hence, you do not need else if statements, but only independent ifs. Try this..

public class checkingDivisibility {
    public static void main(String[] args) {
        int list[] = {1, 2, 3, 6, 8, 10, 12, 14, 15, 17, 19, 21};
        for (int x : list) {
            if (x % 2 == 0) {
                System.out.println(x + "div by 2 possible");
            } 
            if (x % 3 == 0) {
                System.out.println(x + "div by 3 possible");
            }  
            if (x % 5 == 0) {
                System.out.println(x + "div by 5 possible");
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Adding an explanation would be much more helpful for OP.
4

Instead of a single if-else if... condition use separate conditions :

        if (x % 2 == 0) {
            System.out.println(x + "div by 2 possible");
        }
        if (x % 3 == 0) {
            System.out.println(x + "div by 3 possible");
        } 
        if (x % 5 == 0) {
            System.out.println(x + "div by 5 possible");
        }

This way all three conditions will be evaluated in each iteration of the loop.

Of course you'll need to do something smarter if you want an output like 6 is divisible by 2 and 3. You can achieve this with a boolean variable.

        boolean divFound = false;
        if (x % 2 == 0) {
            divFound = true;
            System.out.print(x + "is divisible by 2");
        }
        if (x % 3 == 0) {
            if (!divFound) {
                System.out.print(x + "is divisible by 3");
            } else {
                System.out.println(" and 3");
                divFound = true;
            }
        } 
        if (x % 5 == 0) {
            if (!divFound) {
                System.out.print(x + "is divisible by 5");
            } else {
                System.out.print(" and 5");
                divFound = true;
            }
        }
        if (divFound) {
            System.out.println();
        }

3 Comments

Why is this more efficient? This one will always do 3 condition checks one after the other. The original one will only do one in the best case, 3 in the worst.
@Achilles did you read the question. The OP specifically asks for this.
Ahh I see, yup it worked. Is my thinking correct, so say its in an index where it has the element 6 then with just if it will try to match all the conditions? where with else if only the first suited condition will be use and the other condition is disregarded?
3
 if (x % 2 == 0) {
            System.out.println(x + "div by 2 possible");
        }
        if (x % 3 == 0) {
            System.out.println(x + "div by 3 possible");
        } 
        if (x % 5 == 0) {
            System.out.println(x + "div by 5 possible");
        }

You get the following:

2div by 2 possible 3div by 3 possible 6div by 2 possible 6div by 3 possible 8div by 2 possible 10div by 2 possible 10div by 5 possible 12div by 2 possible 12div by 3 possible 14div by 2 possible 15div by 3 possible 15div by 5 possible 21div by 3 possible

I think it works for you.

Comments

2

You are using if...else if which means if first condition is met then it will not look second condition. Same for further conditions. Beacuse of that your output only shows "divisible by 2" as it only checks first condition and never goes on second or further.

So, you sholuld use if on all conditions if you want to check all conditions.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.