Java Data Type How to - Read console input until y is typed








Question

We would like to know how to read console input until y is typed.

Answer

import java.util.Scanner;
//from  w w w. j  av  a2  s .c  om
public class Main {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    String confirm = "y";

    do {
      System.out.println("Welcome to Java Programming!");
      System.out.println("Print Again? (y/n)");
      confirm = input.nextLine();
    } while (confirm.equalsIgnoreCase("y"));
  }
}

The code above generates the following result.