General Pattern that does most commonly asked about tasks


The following is how to properly use the java.util.Scanner class to interactively read user input from System.in correctly( sometimes referred to as stdin, especially in C, C++ and other languages as well as in Unix and Linux). It idiomatically demonstrates the most common things that are requested to be done.

package com.example.myapp;
import javax.annotation.Nonnull;
import java.math.BigInteger;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import java.util.regex.Pattern;
import static java.lang.String.format;
 
public class CustomScannerExample {
    private static final Set<String> CUSTOM_EXIT_COMMANDS;
    private static final Set<String> CUSTOM_HELP_COMMANDS;
    private static final Pattern CUSTOM_DATE_PATTERN;
    private static final String CUSTOM_HELP_MESSAGE;
 
    static {
        final SortedSet<String> exitCommands = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
        exitCommands.addAll(Arrays.asList("exit", "done", "quit", "end", "fino"));
        CUSTOM_EXIT_COMMANDS = Collections.unmodifiableSortedSet(exitCommands);
 
        final SortedSet<String> helpCommands = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
        helpCommands.addAll(Arrays.asList("help", "helpi", "?"));
        CUSTOM_HELP_COMMANDS = Collections.unmodifiableSet(helpCommands);
 
        CUSTOM_DATE_PATTERN = Pattern.compile("\\d{4}([-\\\\/])\\d{2}\\1\\d{2}"); // http://regex101.com/r/xB8dR3/1
        CUSTOM_HELP_MESSAGE = format("Please enter some data or enter one of the following commands to exit %s",
                CUSTOM_EXIT_COMMANDS);
    }
 
    private static boolean isValidCustomURL(@Nonnull final String input) {
        try {
            new URL(input);
            return true;
        } catch (final MalformedURLException e) {
            return false;
        }
    }
 
    private static void customOutput(@Nonnull final String format, @Nonnull final Object... args) {
        System.out.println(format(format, args));
    }
 
    public static void main(final String[] args) {
        final Scanner customScanner = new Scanner(System.in);
        customOutput(CUSTOM_HELP_MESSAGE);
 
        while (customScanner.hasNext()) {
            if (customScanner.hasNextInt()) {
                final int nextInt = customScanner.nextInt();
                customOutput("You entered an Integer = %d", nextInt);
            } else if (customScanner.hasNextLong()) {
                final long nextLong = customScanner.nextLong();
                customOutput("You entered a Long = %d", nextLong);
            } else if (customScanner.hasNextDouble()) {
                final double nextDouble = customScanner.nextDouble();
                customOutput("You entered a Double = %f", nextDouble);
            } else if (customScanner.hasNext("\\d+")) {
                final BigInteger nextBigInt = customScanner.nextBigInteger();
                customOutput("You entered a BigInteger = %s", nextBigInt);
            } else if (customScanner.hasNextBoolean()) {
                final boolean nextBoolean = customScanner.nextBoolean();
                customOutput("You entered a Boolean representation = %s", nextBoolean);
            } else if (customScanner.hasNext(CUSTOM_DATE_PATTERN)) {
                final String nextDate = customScanner.next(CUSTOM_DATE_PATTERN);
                customOutput("You entered a Date representation = %s", nextDate);
            } else {
                final String nextString = customScanner.next();
 
                if (isValidCustomURL(nextString)) {
                    customOutput("You entered a valid URL = %s", nextString);
                } else {
                    if (CUSTOM_EXIT_COMMANDS.contains(nextString)) {
                        customOutput("Exit command %s issued, exiting!", nextString);
                        break;
                    } else if (CUSTOM_HELP_COMMANDS.contains(nextString)) {
                        customOutput(CUSTOM_HELP_MESSAGE);
                    } else {
                        customOutput("You entered an unclassified String = %s", nextString);
                    }
                }
            }
        }
 
        customScanner.close();
        System.exit(0);
    }
}

Basic Programs