Reputation: 25
Whenever I try to run my code it prints "Please enter number of hours"
infinitely until it gives a stackoverflow
.
public static void hoursToDaysAndHours() throws InterruptedException {
try {
System.out.println("Please enter number of hours");
Calc.fNum = Calc.input.nextDouble();
}
catch (Exception e) {
hoursToDaysAndHours()
}
Calc.sNum = Calc.fNum / 24;
Calc.awnser = Calc.fNum % 24;
System.out.print((int) Calc.fNum + " hours = " + (int) Calc.sNum + " days and " + (int) Calc.awnser + " hours");
Thread.sleep(5000);
Calc.main(null);
}
Calc
is another class that has main and declares the variables... and this is in class hTDAH...
EDIT: As @Deadpool mentioned in the comments, main repeats itself once after the section is over...
3
Please enter a Celious Number
f
Please enter a Celious Number
f
Please enter a Celious Number
f
Please enter a Celious Number
fd
Please enter a Celious Number
d
Please enter a Celious Number
f
Please enter a Celious Number
dddddddddd
Please enter a Celious Number
/
Please enter a Celious Number
87654
87654.0Celcius is 157809.2 in Fahrenheit
Please enter a number between 1-4
1 = AreaOfTriangle
----------------------------------
2 = HoursToDaysAndHours Calculator
----------------------------------
3 = CelciusToFahrenheit Calculator
----------------------------------
4 = BirthdayGame
----------------------------------
Please enter a number between 1-4
1 = AreaOfTriangle
----------------------------------
2 = HoursToDaysAndHours Calculator
----------------------------------
3 = CelciusToFahrenheit Calculator
----------------------------------
4 = BirthdayGame
----------------------------------
Upvotes: 0
Views: 89
Reputation: 718788
First of all, do not catch Exception. Doing that catches pretty much every possible exception, including all sorts of things that you shoudn't catch .... because you are not expecting them, and you don't know how to recover from them.
Second, if you catch an exception that you are not expecting, you need somehow figure out what it was, and what caused it. The first step is to print (or log) the stacktrace.
Thirdly, if you do get a stacktrace you need to read what it says. Or at least, show it to someone who can read what it says. (For example, us!)
So what is actually going on?
Well there are a number of possible explanations, and here are some of them:
If input
is null
then
Calc.input.nextDouble();
will throw a NullPointerException
. If you catch that exception and try again (as your are doing), that will loop.
If the next token in the scanner's input stream is NOT a valid double, then nextDouble()
will throw an InputMismatchException
without advancing past the token. If you catch and retry, you will just get the same exception ... and you will loop.
There are potentially other exceptions that could occur here and give the same outcome; e.g. NoSuchElementException
and IllegalStateException
.
A different scenario is that you could be looping because of the
Calc.main(null);
call at the end of the method. That will most likely call your Calc
classes entry point again ... causing you to loop.
Now the stacktrace from your stack overflow should allow you to distinguish some of these, and adding e.printStacktrace()
to the handler would provide more.
Challenge: try doing this / reading the stack traces to see if you can work it out for yourself!
... so my understanding is that i have to cast input to a double
No. The input
variable is presumably a Scanner
. You can't cast a Scanner
to a double.
Based on what I said above, I / we cannot (yet) tell you what you need to do to fix this. But the first step is to print the stacktrace and read it!
But what does
Double.parseDouble(Calc.input.next())
do thatCalc.input.nextDouble()
cannot do.
They use different syntax rules:
Scanner
parses numbers according to a syntax that depends on the current locale settings; see javadoc.Double.parseDouble
parses numbers according to a locale insensitive syntax; see javadoc.This could be sufficient for a string to be valid in one case and not the other. It depends on the string you are trying to parse ... and your locale.
Upvotes: 1
Reputation: 895
One possible source of error:
In The line Calc.fNum = Calc.input.nextDouble();
you are trying to read an input from the standard input stream and telling that the input should be parsed as a double. If the input cannot be parsed as a double then an exception occurs.
Now looking at your catch block, you are catching all exceptions ( catch(Exception e)
). But instead of handling the exception, you are doing a recursive call to the same function within the catch block.
So if your input is not something that can be parsed as double then the exception keeps occurring and every time the exception is being caught and another recursive call to the function hoursToDaysAndHours()
happens within the catch block.
} catch (Exception e) {
hoursToDaysAndHours() // Recursive call to the same function.
}
So you are never going beyond the catch block if your input is not something that can be parsed as double
.
Each recursive call to the function will save the current execution onto the stack. This goes on until you run out of stack memory and hence you end up with a StackOverflowException
.
Update: A simple way to fix the problem and accomplish what you are trying to do:
public class HoursToDayConverter {
public static void hoursToDaysAndHours() throws InterruptedException {
try {
System.out.println("Please enter number of hours");
Calc.fNum = Double.parseDouble(Calc.input.next());
} catch (NumberFormatException e) {
System.out.println("Invalid input. Try again.");
hoursToDaysAndHours();
}
Calc.sNum = Calc.fNum / 24;
Calc.awnser = Calc.fNum % 24;
System.out.print((int) Calc.fNum + " hours = " + (int) Calc.sNum + " days and " + (int) Calc.awnser + " hours\n");
Thread.sleep(5000);
Calc.main(null);
}
}
Upvotes: 0