Source Code : Use Scanner to read user input

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

Use Scanner to read user input

  
import java.util.Scanner;

class Console {

  static Scanner sc = new Scanner(System.in);

  public static boolean askYorN(String prompt) {
    while (true) {
      String answer;
      System.out.print("
" + prompt + " (Y or N) ");
      answer = sc.next();
      if (answer.equalsIgnoreCase("Y"))
        return true;
      else if (answer.equalsIgnoreCase("N"))
        return false;
    }
  }
}

public class MainClass {
  public static void main(String[] args) {
    while (Console.askYorN("Keep going?")) {
      System.out.println("!");

    }
  }
}

   
  

Thank with us