0

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; 

        }
    }

2 Answers 2

3

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");
}
Sign up to request clarification or add additional context in comments.

4 Comments

You might need to use try catch as well ?
@almasshaikh That depends, NumberFormatException is a RuntimeException and you should avoid using try-catch blocks to control logic flow. Not saying you should never do it, you just need to provide the use-case justificaiton for it...
It's edge cases that OP wants to cover and hence i asked for the same :)
@almasshaikh This is tre, as I said, you need to make the justification on each case, you could, for example use a regular expression to test for a numeric value before doing the conversion
2

You have to catch NumberFormatException, since that's the exception that would be thrown if you call parseInt on an empty String:

double x = -1;
try {
    x = Integer.parseInt(ab);
}
catch (NumberFormatException exc) {
    x = -1;
}

You can check if the String is empty prior to calling parseInt, but then you might still get this exception if the user enters some non-numeric characters or a too large number.

3 Comments

This is a personal nit pick, so I don't think the answer is "wrong", but you should generally try an avoid using try-catch for branching logic
@MadProgrammer To avoid having to catch the exception entirely, you'll need to implement some of the logic that parseInt already contains - check that all characters of the String are digits, and that the number is not too large (or too small) to fit in an int.
That's true. Considering NumberFormatException is a RuntimeException, it also makes it more dangerous to trap it. As a general rule of thumb, it's unadvisable to use try-catch blocks to control program logic, not saying your NEVER should do it, just that's it's unadvisable. You could equally use a regular expression, for example, to do a quick check on the validity of the String

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.