JAVA PROGRAMMING:
Write an input validation loop (i.e. a loop that checks to see whether the input received from the user is valid) that asks the user to enter the word “yes” or “no”.
import java.util.Scanner; public class InputValidation { public static void main(String[] args) { Scanner in = new Scanner(System.in); String choice; while (true) { System.out.print("Enter either yes or no: "); choice = in.nextLine(); if (choice.equalsIgnoreCase("yes") || choice.equalsIgnoreCase("no")) break; System.out.println("Invalid choice. Try again!"); } System.out.println("You entered a valid value of " + choice); } }
Get Answers For Free
Most questions answered within 1 hours.