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

intnumberOfLines(String text)
number Of Lines
Scanner scanner = new Scanner(text);
int i = 0;
while (scanner.hasNextLine()) {
    scanner.nextLine();
    i++;
scanner.close();
return i;
...
voidOperasiMasukan()
Operasi Masukan
Scanner input = new Scanner(System.in);
System.out.print("Masukkan sebuah bilangan: ");
if (input.hasNextInt()) {
    System.out.println(input.nextInt());
} else {
    System.out.println("Bukan bilangan");
intparameterInput(int start, int end)
parameter Input
Scanner in = new Scanner(System.in);
int choice = 0;
boolean done = false;
while (!done) {
    try {
        choice = in.nextInt();
    } catch (InputMismatchException e) {
        in.nextLine();
...
Listparse_str_vector(String str_vector)
parsstvector
List<Double> vector = new ArrayList<Double>();
Scanner scanner = new Scanner(str_vector.substring(1, str_vector.length() - 1));
scanner.useDelimiter(", ");
while (scanner.hasNextDouble())
    vector.add(scanner.nextDouble());
return vector;
voidparseDailyData(final List lines, final List priceValues, final List dailyPriceDate)
parse Daily Data
for (final String line : lines) {
    final Scanner lineScan = new Scanner(line);
    lineScan.useDelimiter(",|\t");
    dailyPriceDate.add(lineScan.next());
    priceValues.add(lineScan.next());
    lineScan.close();
StringparseExperimentName(String name)
Parses the experiment name.
Scanner scanner = new Scanner(name);
StringBuilder csvName = new StringBuilder();
scanner.useDelimiter("_");
for (int i = 0; i < 4; i++) {
    if (scanner.hasNext()) {
        csvName.append(scanner.next() + ",");
    } else {
        csvName.append(",");
...
LinkedHashMap>parseQueryString(String queryString)
parse Query String
final Scanner scanner = new Scanner(queryString);
scanner.useDelimiter("&");
LinkedHashMap<String, List<String>> res = new LinkedHashMap<String, List<String>>();
while (scanner.hasNext()) {
    final String[] nameValue = scanner.next().split("=");
    if (nameValue.length == 0 || nameValue.length > 2) {
        throw new IllegalArgumentException("bad parameter");
    final String name = nameValue[0];
    String value = null;
    if (nameValue.length == 2) {
        value = nameValue[1];
    if (value == null) {
        value = "";
    putVarVal(res, name, value);
return res;
longparseTime(String input)
parse Time
Scanner scanner = new Scanner(input.trim());
scanner.useLocale(Locale.ENGLISH);
long time;
if (input.contains(":")) {
    scanner.useDelimiter(":");
    if (!scanner.hasNextLong()) {
        return 0;
    long minutes = scanner.nextLong();
    if (minutes < 0) {
        return 0;
    if (!scanner.hasNextDouble()) {
        return 0;
    double seconds = scanner.nextDouble();
    if (seconds < 0.0 || seconds >= 60.0) {
        return 0;
    time = (long) (60000 * minutes + 1000 * seconds);
else {
    if (!scanner.hasNextDouble()) {
        return 0;
    double seconds = scanner.nextDouble();
    if (seconds < 0.0) {
        return 0;
    time = (long) (1000 * seconds);
return time;
ListpathTokenList(String path)
path Token List
LinkedList<String> resultList = new LinkedList<String>();
Scanner scanner = new Scanner(path);
scanner.useDelimiter("\\\\");
if (path.startsWith("\\\\")) {
    int endPos = path.indexOf("\\", 2);
    resultList.add(path.substring(0, endPos));
    scanner.next();
    scanner.next();
...
voidpressEnterToContinue()
Pause program and prompt user to press enter to continue program execution
System.out.println("\n\nPress enter to continue...");
keyboard.nextLine();