Add a getChoice() method to the Validation class: a. Signature of the class is:
ID: 3594392 • Letter: A
Question
Add a getChoice() method to the Validation class:
a. Signature of the class is: public static void getChoice(Scanner sc, String enterMsg, String vaildChoices)
b. Where the String parameter enterMsg is the user prompt - Ex: Continue (y/n): ?
c. Where the String parameter validChoices is a string that contains all of the valid letter choice answers for the prompt - Ex: YyNn
d. Use the String.contains() method to determine if the choice the user enters is valid: if(validChoices.contains(UserChoice)) isValid = true;
Explanation / Answer
import java.util.Scanner;
class Validation {
// method that makes isValid true if the entered value by user is valid
public void getChoice(Scanner sc, String enterMsg, String vaildChoices)
{
boolean isValid = false;
// taking user input
System.out.print(enterMsg);
String UserChoice = sc.next();
// using contains method
if(vaildChoices.contains(UserChoice)) isValid = true;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.