Java Scanner Usage queryMenu(Scanner in, String msg, LinkedList options)

Here you can find the source of queryMenu(Scanner in, String msg, LinkedList options)

Description

query Menu

License

Open Source License

Declaration

public static int queryMenu(Scanner in, String msg, LinkedList<String> options) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.LinkedList;
import java.util.Scanner;

public class Main {
    public static int queryMenu(Scanner in, String msg, LinkedList<String> options) {
        System.out.printf("%s\n", msg); // print out the message to stdout

        while (true) {
            int idx = 0;
            for (String option : options) {
                System.out.printf("\t%d. %s\n", idx, option);
                idx++;//www  .j  a  va 2s  . c  o m
            }

            System.out.printf("Enter an option from the list above: ");
            String response = in.nextLine();

            for (String opt : options) {
                if (response.equals(opt)) {
                    return options.indexOf(response);
                } else {
                    int result = Integer.parseInt(response);
                    try {
                        options.get(result);
                        return result;
                    } catch (IndexOutOfBoundsException e) {
                        System.out.printf("No element exists at %d", result);
                    }
                }
            }
            System.out.printf("Input did not match any option. Try again.\n");
        }
    }

    public static int queryMenu(Scanner in, String msg) {
        System.out.printf("%s\n", msg);
        String response = in.nextLine();
        return Integer.parseInt(response);

    }
}

Related

  1. parseQueryString(String queryString)
  2. parseTime(String input)
  3. pathTokenList(String path)
  4. pressEnterToContinue()
  5. processLine(String aLine)
  6. queryStr(Scanner in, String msg)
  7. removeFirstNLines(String text, int n)
  8. removeIndexes(String xpath)
  9. removeNLinesForwardsFromStringMatch(String text, String match, int n)