I want my code to catch if the user didn't input anything. I can't use ab.equals(null), ab.isEmpty() because I convert the String to integer as you can see.
import javax.swing.JOptionPane;
public class Mango {
public static void main (String[] args){
String ab = JOptionPane.showInputDialog("Enter Anything");
double sum = 0;
double x = Integer.parseInt(ab);
if(x <= 0){
System.out.println("Empty!");
}else{
sum+=x;
}
}
Check the return value BEFORE you try converting it...
String ab = JOptionPane.showInputDialog("Enter Anything");
if (ab != null && !ab.trim().isEmpty()) {
// Try and convert the value...
} else {
// Bad input
}
Example
double sum = 0;
String ab = JOptionPane.showInputDialog("Enter Anything");
if (ab != null && !ab.trim().isEmpty()) {
double x = Integer.parseInt(ab);
sum += x;
} else {
System.out.println("\"" + ab + "\" is not a valid input value");
}