java code to input non blank string

java code to input non blank string

To input a non-blank string in Java, you can use a loop that reads strings from the user until a non-blank string is entered.

Here is an example of how to input a non-blank string in Java:

refe‮ual:ot r‬tturi.com
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    String input = "";
    while (input.trim().isEmpty()) {
      System.out.print("Enter a non-blank string: ");
      input = scanner.nextLine();
    }

    System.out.println("You entered: " + input);

    scanner.close();
  }
}

In this example, the Scanner class is used to read strings from the user using the nextLine method. The while loop continues to read strings until a non-blank string is entered, which is determined by checking if the string is empty after removing leading and trailing whitespace using the trim method.

Once a non-blank string is entered, it is output to the console using the println method.

Created Time:2017-11-03 00:14:50  Author:lautturi