Answer the question
In order to leave comments, you need to log in
The new method throws a lot of true/false followed by "Exception in thread "main" java.lang.StackOverflowError". How to fix?
I want to write an isNegative() method that takes a number and checks if it is negative/positive.
After compilation, the code gives out something like 5000 false and a thousand more "at Learning6.isNegative(Learning6.java:14)"
What could be the problem?
public class Learning6 {
public static void main(String[] args) {
isNegative(-50);
}
public static boolean isNegative(int number) {
if (number < 0) {
System.out.println("false");
} else {
System.out.println("true");
}
return isNegative(number);
}
Answer the question
In order to leave comments, you need to log in
The problem is that the method endlessly calls itself without any conditions.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question