Eli's answer will work fine. I just send the answer to your question (comment) to Eli's answer.
Can you explain what a try-catch block is?
You must first understand what an exception is. An exception is an event that occurs during the execution of your program, which violates the normal flow of program instructions.
There are 3 types of exceptions in Java:
Runtime exceptions : An exception is called a runtime exception if its data type is java.lang.RuntimeException or its subclass.
Excluded exceptions: An exception is called a checked exception if its data type is a child of java.lang.Exception , but not a child of RuntimeException .
Errors: An exception is called an error if its data type is a child of the java.lang.Error class. The error is related to problems that occur outside your application and usually does not try to repair the errors.
For a more detailed description of exceptions, read this .
To catch and handle these exceptions, you use try-catch blocks. For example, you use the Double.parseDouble function. If the parameter in this function is not a valid number, for example, if the user set the string "NotANumber", you will try to convert it to double, then NumberFormatException will be Double.parseDouble . If you cannot handle this error, your program will exit unexpectedly.
So you should write something like the following (including the positive number function you want):
double imperial1 = 0.0; try { double firstNumber = Double.parseDouble(strValue3); double secondNumber = Double.parseDouble(strValue4); if(firstNumber < 0 || secondNumber < 0) throw new NumberFormatException("numbers must be positive."); imperial1 = firstNumber + secondNumber / 12.0; } catch(NumberFormatException ex) {
source share