advictoriam

Untitled

Jan 8th, 2019
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.56 KB | None | 0 0
  1. public class Inputs
  2. {
  3.    /**
  4.       Gets the choice that the user provided, or null if the
  5.       user didn't provide a valid choice.
  6.       @param input the user input
  7.       @param maxChoice the maximum valid choice, e.g. "D" if there
  8.       are four choices.
  9.       @return the user input if it was a valid choice (i.e. length 1
  10.       and between "A" and maxChoice), null otherwise
  11.    */
  12.    public String getChoice(String input, String maxChoice)
  13.    {
  14.       String regex = "[A-" + maxChoice + "]";
  15.       return (input.matches(regex)) ? input : null;
  16.    }
  17. }
Add Comment
Please, Sign In to add comment