Your logic is a bit off, first off, in Java String
s are compared using equals(..)
:
if(amountEntered != null){
amntEntered = Double.parseDouble(amountEntered);
// Because you are comparing amountEntered to "" and you check if it's null
//I assume it is of type String which means that you can't cast it to a double.
}else if(!amountEntered.equals("")){
// if it gets past the first check
// it means that amountEntered is null and this will produce a NullPointerException
amntEntered = Double.parseDouble(amountEntered);
}else if((amountEntered == null || amountEntered.equals(""))){
// Here if amountEntered is null, the second check will
// still be executed and will produce a NullPointerException
// When you use || both the check before || and after || are executed
System.out.print("");
}
Here is how you can perform the check and deal with any Exception
s:
if(amountEntered != null && !amountEntered.isEmpty()){
try{
someDoubleVariable = Double.parseDouble(amountEntered);
}catch(NumberFormatException e){
someDoubleVariable = 0;
e.printStackTrace()
}
}else if(amountEntered==null || (amountEntered!=null && amountEntered.isEmpty())){
someDoubleVariable = 0;
}
In this example, because I'm using &&
the condition checking will stop as soon as one of them is false
which means that in the last else if
if amountEntered
is null amountEntered.isEmpty()
will not be executed