Rathin Rathin - 3 months ago 7
Java Question

How can I compare an empty input using an if statement in java?

I want to compare an empty input using an if statement, basically the user just pressing enter when entering input and not any value.

Answer

you can use the following code

    String input = "   ";
    if (input == null || "".equals(input.trim())) {
        System.out.println("input is empty");
    } else {
        System.out.println("input is not empty: " + input);
    }