public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = scanner.nextInt();
System.out.println("your result is " + result(a,b,c));
}
public static int result(int x, int y, int f) {
int result = 0;
result = switch (f) {
case 1 -> x + y;
case 2 -> x - y;
case 3 -> x * y;
case 4 -> x / y;
default -> 0;
};
return result;
}
}
the calculator would break when 0 is divided by zero so i’m trying to catch that
if(x != 0 && y != 0) {
result = switch (f) {
case 1 -> x + y;
case 2 -> x - y;
case 3 -> x * y;
case 4 -> x / y;
default -> 0;
};
}
i’m trying to catch it using if statement with two conditions, but the methods return the default value even if only either x or y is zero instead of both, what am i doing wrong?
hasan muttaqin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
but the methods return the default value even if only either x or y is zero instead of both, what am i doing wrong?
That’s because.. that’s what you wrote!
Your code says: If both (&&
means ‘and’) x
is not equal to 0 and y
is not equal to zero, do the usual thing.
In other words, if either x is 0, or y is 0, do not do the usual thing.
That answers your question. But the intent behind your idea is wrong (as in, you had an idea – I will not do the usual operation if either x is 0 or y is 0 – and you asked SO questions about implementing this idea. That’s because you made 2 mistakes: You had an idea that does not work, and then also misimplemented the idea).
That’s because e.g. 0 + 0
is perfectly fine. In fact, even 0 / 1
is fine (that is simply 0). You only have a divide by 0 issue if you are actually dividing, and only if y is 0.
Thus, what you really want, is to only in the case of division, and only in the case y
is 0, do something else:
result = switch (f) {
case 1 -> x + y;
case 2 -> x - y;
case 3 -> x * y;
case 4 -> y == 0 ? 0 : x / y;
default -> 0;
};
Note that this setup is unjavalike. It works, but most java programmers would strongly dislike this code. Java (the language and the ecosystem) tends to deal with unexpected situations by throwing exceptions, not by just doing random weird stuff (and returning 0 is certainly random/weird. It certainly isn’t the correct answer, in fact, it’s as far away from correct as you can get, returning 0 for dividing by 0. Dividing by 0 tends to produce large numbers, not small ones. Infinitely large ones, in fact). Thus, what you really want is that ArithmaticException
, that’s fine. And your default
case should similarly throw something – your software is designed such that f
can never be anything but 1/2/3/4, so if the impossible happens, an exception is exactly what you want. Taking it even further than that, those calculator buttons should be defined as an enum – but I’m sure that’ll be a lesson for later.
1
You can catch ArithmeticException following this example:
try {
result = switch (f) {
case 1 -> x + y;
case 2 -> x - y;
case 3 -> x * y;
case 4 -> x / y;
default -> 0;};
} catch (ArithmeticException e) {
System.out.println("Error: Division by zero is not allowed.");
result = -1;
}
return result;