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

String[]getExtensions(Scanner scanner)
Function that returns the extensions available in file open into the given Scanner
List<String> extensions = new ArrayList<String>();
while (scanner.hasNextLine()) {
    String line = scanner.nextLine().trim();
    if (line.startsWith(".")) {
        line = line.substring(1, line.length());
    if (!line.isEmpty()) {
        extensions.add(line);
...
IntegergetFirstInt(String s)
get First Int
Scanner in = new Scanner(s).useDelimiter("[^0-9]+".intern());
if (in.hasNext()) {
    return in.nextInt();
} else {
    return null;
StringgetInput(Scanner in, String message, boolean allowNull)
get Input
while (true) {
    System.out.print(message);
    String input = in.nextLine();
    input = input.trim();
    if (input.length() > 1) {
        System.out.println("");
        return input;
    if (allowNull) {
        System.out.println("");
        return null;
intgetInt()
Returns an int from standard input, ignoring input that is not an int
int anInt = 0;
try {
    anInt = keyboard.nextInt();
} catch (InputMismatchException ex) {
keyboard.nextLine();
return anInt;
intgetInt(Scanner sc)
get Int
int result = sc.nextInt();
sc.nextLine();
return result;
intgetInteger(String parameter)
Get the integer value from a string.
String withDots = withDot(parameter);
Scanner stringScanner = new Scanner(withDots);
stringScanner.useLocale(Locale.US);
return stringScanner.nextInt();
ListgetLines(String str)
Returns all lines from the given string, i.e.
String line = null;
List<String> lines = new ArrayList<>();
if (str.isEmpty()) {
    lines.add("");
    return lines;
Scanner scanner = new Scanner(str);
while (scanner.hasNextLine()) {
...
StringgetLineStartingWith(Scanner scanner, String starts)
Return the line starting with starts using the given scanner.
if (scanner != null) {
    while (scanner.hasNextLine()) {
        String line = scanner.nextLine().trim();
        if (line.startsWith(starts)) {
            scanner.close();
            return line;
    scanner.close();
return "";
longgetLngIn()
This returns a long number from user input
return sc.nextLong();
StringgetNextInput(Scanner scan)
read next input string
String word = scan.nextLine();
String word2 = (word.split(SEPARATOR)[0]);
return word2.trim();