Keeping a loop to test multiple conditions - Java
I have an array with integers inside it. I want to iterate over them to check if it is divisible by 2, 3, 5. At the moment, my code only runs once.
So tell me if I have 6 on the list. It will only return "6 divides by 2" where it should be "6 divides by 2 and 3"
So how do I make my code more elegant. Is there a way to write code without having to define how if (number % 2 == 0) && (number % 3 == 0)...
, or should it be done this way? each time defining each of the conditions.
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");
}
}
}
}
source to share
You have else if
after if
, which means the next condition if
is only evaluated if the first was false
. This is not what you want.
You want every condition to be checked. Hence, you don't need operators else if
, but only independent if
s. Try it.
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");
}
}
}
}
source to share
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");
}
Thus, all three conditions will be evaluated at each iteration of the loop.
Of course, you need to do something smarter if you want to get results 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();
}
source to share
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 maybe 3div by 3 possible 6div by 2 maybe 6div by 3 maybe 8div by 2 maybe 10div by 2 10div by 5 possible Maybe 12div by 2 Maybe 12div by 3 14div by 2 maybe 15div by 3 possible 15div by 5 possible 21div by 3 possible
I think it will work for you.
source to share
You use if ... else if it means that if the first condition is met, it won't look like the second condition. The same goes for further conditions. Due to the fact that your output only shows "divisible by 2" as it only checks the first condition and never goes to the second or later.
So, you use sholuld if all conditions, if you want to check all conditions.
source to share