Java examples for Language Basics:Console
ask Yes Or No from Console
//package com.java2s; import java.util.Scanner; public class Main { public static void main(String[] argv) throws Exception { String query = "java2s.com"; System.out.println(askYesOrNo(query)); }// w ww.j a v a 2 s . c o m public static boolean askYesOrNo(String query) { String yesOrNo = null; boolean validInput = false; while (!validInput) { System.out.println(query); yesOrNo = getConsoleInput(); validInput = yesOrNo.toUpperCase().equals("Y") || yesOrNo.toUpperCase().equals("N"); if (!validInput) { System.out .println("Invalid input! Please enter /'Y/' or /'N/'"); } } return yesOrNo.toUpperCase().equals("Y"); } public static String getConsoleInput() { Scanner scanner = new Scanner(System.in); return scanner.nextLine(); } }