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

ListasList(String source, String separator)
as List
Scanner scanner = new Scanner(source);
scanner.useDelimiter(separator);
List<String> list = new ArrayList<String>();
while (scanner.hasNext()) {
    list.add(scanner.next());
return list;
intcalcCompileWarnings(String compilerResult)
calc Compile Warnings
int warnings = 0;
Scanner scanner = new Scanner(compilerResult);
while (scanner.hasNextLine()) {
    if (scanner.nextLine().contains("warning")) {
        warnings++;
return warnings;
...
StringcleanUpEmptyLinesAndIndent(String input)
clean Up Empty Lines And Indent
StringBuffer output = new StringBuffer();
Scanner scanner = new Scanner(input);
while (scanner.hasNextLine()) {
    String line = scanner.nextLine();
    line = line.replaceAll("\t", " ");
    while (line.contains("  ")) {
        line = line.replaceAll("  ", " ");
    line = line.trim();
    if (line.length() > 0) {
        output.append(line);
        output.append("\n");
return output.toString();
voidclose()
close
Scanner keyboardScanner = getThreadScanner();
if (keyboardScanner != null) {
    keyboardScanner.close();
    keyboardScanner = null;
Stringcommon(Scanner in)
common
String a = in.nextLine();
String b = in.nextLine();
boolean[] marked = new boolean[26];
for (char c : a.toCharArray()) {
    int pos = c - 97;
    marked[pos] = true;
for (char c : b.toCharArray()) {
...
int[][]computeSumTable(Scanner in, short n)
compute Sum Table
int[][] matrix = new int[n][n];
int xSum = 0;
for (int x = 0; x < n; x++) {
    xSum += in.nextInt();
    matrix[x][0] = xSum;
for (int y = 1; y < n; y++) {
    xSum = 0;
...
booleanconfirm(String message)
Ask user to confirm an action.
System.out.print(message + " (yes/no): ");
String answer = in.nextLine().toLowerCase().trim();
if (answer.equals("yes") || answer.equals("y"))
    return true;
else if (answer.equals("no") || answer.equals("n"))
    return false;
return confirm(message);
booleanconfirm(String message)
confirm
out(NEW_LINE, message + " (Y/N): ");
String response = scanner.nextLine();
return response != null && response.trim().toUpperCase().equals("Y");
booleanconfirmAction(String warning)
confirm Action
System.out.println(warning);
System.out.print("Are you sure you want to do this? (yes/no): ");
Scanner in = new Scanner(System.in);
while (true) {
    try {
        String input = in.nextLine();
        if ("yes".equals(input)) {
            return true;
...
intcountFileLines(Scanner fin)
Counts the number of lines that are in an input file.
int count = 0;
while (fin.hasNext()) {
    count++;
    fin.nextLine();
return count;