Code
int weight = 0;
do {
System.out.print("Weight (lb): ");
weight = Integer.parseInt(console.nextLine());
if (weight <= 0) {
throw new IllegalArgumentException("Invalid weight.");
}
} while (weight <= 0);
Traceback
Weight (lb): Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.base/java.lang.Integer.parseInt(Integer.java:662)
at java.base/java.lang.Integer.parseInt(Integer.java:770)
at HealthPlan.main(HealthPlan.java:46)
When I run my program, I get this exception. How do I handle it?
I want to input an integer as a weight
value. I also have to use an integer value for height
, but my program asks for input that are boolean
s and character
s as well.
Someone suggested that I should use Integer.parseInt
.
If I need to post more code, I'd be happy to do so.