11package utils ;
22
3+ import java .util .Arrays ;
34import java .util .Collection ;
5+ import java .util .HashSet ;
46
57/**
68 * Additional classes and methods to support more features like C/C++ like style coding
@@ -27,6 +29,40 @@ public static int scanValidInteger(String prompt, FunctionPointer<Boolean, Integ
2729 }
2830 }
2931
32+ public static double scanValidDouble (String prompt , FunctionPointer <Boolean , Double > callback ) {
33+ double returnValue ;
34+ for (;;) {
35+ if (callback .invoke (returnValue = ScannerInput .validNextDouble (prompt ))) {
36+ return returnValue ;
37+ }
38+ System .out .println ("Sorry, that is an invalid value, please enter again!" );
39+ }
40+ }
41+
42+ public static String scanValidString (String prompt , FunctionPointer <Boolean , String > callback ) {
43+ String returnValue ;
44+ for (;;) {
45+ if (callback .invoke (returnValue = ScannerInput .validNextLine (prompt ))) {
46+ return returnValue ;
47+ }
48+ System .out .println ("Sorry, that is an invalid value, please enter again!" );
49+ }
50+ }
51+
52+ public static String scanValidString (String prompt , Collection <String > collection ) {
53+ String returnValue ;
54+ for (;;) {
55+ if (collection .contains (returnValue = ScannerInput .validNextLine (prompt ).toLowerCase ())) {
56+ return returnValue ;
57+ }
58+ System .out .println ("Sorry, that is an invalid value, please enter again!" );
59+ }
60+ }
61+
62+ public static String scanValidString (String prompt , String [] array ) {
63+ return scanValidString (prompt , new HashSet <>(Arrays .asList (array )));
64+ }
65+
3066 /**
3167 * A helper class that helps to count, sum, calculate, etc.
3268 * @param <E> the type of the elements that the collection contains
0 commit comments