Use Scanner to read user input : Scanner « Development Class « Java
- Java
- Development Class
- Scanner
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("\n" + 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("!");
}
}
}
Related examples in the same category