Check whether a String is not Null and not Empty

To check if a string is not null and not empty in Java, you can use the length() method of the java.lang.String class to check if the string is empty, and the != operator to check if the string is not null. Here is an example of how you can do this:

import java.util.Scanner;

public class StringNotNullNotEmptyExample {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.print("Enter a string: ");
    String input = in.nextLine();
    if (input != null && input.length() > 0) {
      System.out.println("The string is not null and not empty");
    } else {
      System.out.println("The string is null or empty");
    }
  }
}

This example prompts the user to enter a string, and then checks if the string is not null and not empty. If the string is not null and not empty, it prints "The string is not null and not empty". If the string is null or empty, it prints "The string is null or empty".

You can also use the isEmpty() method of the java.lang.String class to check if the string is empty, like this:

import java.util.Scanner;

public class StringNotNullNotEmptyExample {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.print("Enter a string: ");
    String input = in.nextLine();
    if (input != null && !input.isEmpty()) {
      System.out.println("The string is not null and not empty");
    } else {
      System.out.println("The string is null or empty");
    }
  }
}

This example works the same way as the previous example, but uses the isEmpty() method to check if the string is empty. The isEmpty() method returns true if the string is empty, and false if it is not.