Java Utililty Methods Scanner Read

List of utility methods to do Scanner Read

Description

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

Method

StringreadFromScanner(Scanner scanner)
read From Scanner
if (!scanner.hasNext()) {
    return new String();
return scanner.useDelimiter(REGEX_BEGINNING_OF_TEXT).next();
intreadInt(String prompt)
Writes a prompt to standard out and tries to read an int value from standard in.
int value = 0;
boolean intRead = false;
do {
    System.out.print(prompt);
    try (Scanner line = new Scanner(System.in); Scanner scannerLine = new Scanner(line.nextLine());) {
        if (scannerLine.hasNextInt()) {
            intRead = true;
            value = scannerLine.nextInt();
...
StringreadLine(String format, Object... args)
read Line
System.out.print(String.format(format, args));
return new Scanner(System.in).nextLine();
int[][]readMatrix(Scanner in, int n)
read Matrix
int[][] matrix = new int[n][n];
for (int i = 0; i < n; i++) {
    matrix[i] = readArr(in, n);
return matrix;
char[]readPassword(String format, Object... args)
read Password
if (System.console() != null)
    return System.console().readPassword(format, args);
System.out.println("WARNING -> Console password input unsupported, password entry in insecure mode!");
return readLine(format, args).toCharArray();
intreadPositiveInt(String message, Scanner input)
Read in a positive integer, which may come from a configuration file
int value = Integer.parseInt(getNextInput(input));
if (value < 1) {
    throwException("Error.  Illegal configuration parameter: " + message + ": " + value);
return value;
ArrayListreadSystemInToIntArrayList()
read System In To Int Array List
ArrayList<Integer> array = new ArrayList<Integer>();
Scanner sc = new Scanner(System.in);
while (sc.hasNextInt()) {
    array.add(sc.nextInt());
sc.close();
return array;
StringreadWholeString(Scanner s)
read Whole String
return s.useDelimiter("\\A").hasNext() ? s.next() : "";