Java Utililty Methods Scanner Usage

List of utility methods to do Scanner Usage

Description

The list of methods to do Scanner Usage are organized into topic(s).

Method

ListgetStrings(Scanner sc, int n)
get Strings
List<String> result = new ArrayList<>(n);
for (int i = 0; i < n; i++) {
    result.add(sc.nextLine());
return result;
intgetTimeAsInt(String duration)
get Time As Int
if (!isValueValid(duration, PATTERN_TIME) || duration.length() == 0) {
    throw new IllegalArgumentException(duration + " is not a time type");
if (duration.endsWith(":")) {
    duration += " ";
Scanner s = new Scanner(duration);
int res = 0;
...
StringgetTypedValue(String question)
get Typed Value
System.out.println(question);
String msgRead = scan.nextLine();
return msgRead;
Stringinput(String message)
input
out(NEW_LINE, message + ": ");
return scanner.nextLine();
Stringinput(String prompt)
input
String input;
System.out.println(prompt);
Scanner scanner = new Scanner(System.in);
input = scanner.nextLine();
return input.trim();
booleanisInteger(String s)
is Integer
return isInteger(s, 10);
booleanisNumeric(String str)
is Numeric
Scanner scanner = new Scanner(str);
boolean flag = scanner.hasNextInt();
scanner.close();
return flag;
booleanisValueValid(String value, String pattern)
is Value Valid
Scanner s = new Scanner(value);
try {
    s.next("^(" + pattern + ")$");
    return true;
} catch (InputMismatchException ex) {
    return false;
booleanisYes(String str)
is Yes
return getYesNoAsInt(str) == 1 ? true : false;
Iterableiterate(final String node)
Returns an Iterable over a node's path components.
return new Iterable<String>() {
    @Override
    public Iterator<String> iterator() {
        return new Scanner(node).useDelimiter("/");
};