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

voidprocessLine(String aLine)
process Line
Scanner scanner = new Scanner(aLine);
scanner.useDelimiter("=");
if (scanner.hasNext()) {
    String name = (scanner.next()).trim();
    String value = (scanner.next()).trim();
    if (name.equals("IPv4AddrShow")) {
        if (value.equals("n"))
            ipv4AddrShow = false;
...
intqueryMenu(Scanner in, String msg, LinkedList options)
query Menu
System.out.printf("%s\n", msg); 
while (true) {
    int idx = 0;
    for (String option : options) {
        System.out.printf("\t%d. %s\n", idx, option);
        idx++;
    System.out.printf("Enter an option from the list above: ");
...
StringqueryStr(Scanner in, String msg)
query Str
System.out.printf("%s", msg);
String response = in.nextLine();
if (response.length() == 0) {
    System.out.printf("Please enter a valid string!\n");
return response;
StringremoveFirstNLines(String text, int n)
remove First N Lines
Scanner scanner = new Scanner(text);
int i = 0;
while (scanner.hasNextLine() && i < n) {
    scanner.nextLine();
    i++;
String result = "";
while (scanner.hasNextLine()) {
...
StringremoveIndexes(String xpath)
Remove indexes from an xpath string.
final String[] partialSteps = xpath.split("[/]");
if (partialSteps.length == 0) {
    return xpath;
int startIndex = 0;
StringBuffer buf = new StringBuffer();
for (int i = startIndex; i < partialSteps.length; i++) {
    String step = partialSteps[i];
...
StringremoveNLinesForwardsFromStringMatch(String text, String match, int n)
Removes the first n lines within a plain text from the position where the search string is located, inclusive.
Scanner scanner = new Scanner(text);
String line = null, result = "";
while (scanner.hasNextLine()) {
    line = scanner.nextLine();
    if (line.matches("(" + match + ").*")) {
        int i = 0;
        while (scanner.hasNextLine() && i < n) {
            scanner.nextLine();
...
StringselectProject(List projectList)
select Project
if (projectList == null || projectList.isEmpty()) {
    return null;
System.out.println("Gerrit Projects:");
for (int i = 0; i < projectList.size(); i++) {
    System.out.println("[" + i + "] " + projectList.get(i));
System.out.print("Number of project to be deleted? [-1 to quit] ");
...
ListsplitLines(String string)
split Lines
List<String> lines = new ArrayList<String>();
Scanner scanner = new Scanner(string);
while (scanner.hasNextLine()) {
    lines.add(scanner.nextLine());
return lines;
double[]splitToDouble(String input)
split To Double
Scanner s = new Scanner(input);
s.useDelimiter("(,|;)\\s*");
List<Double> values = new ArrayList<Double>();
while (s.hasNext()) {
    if (s.hasNextDouble()) {
        values.add(s.nextDouble());
    } else {
        s.next();
...
double[]stringToArray(String str)
string To Array
Scanner scn = new Scanner(str);
scn.useLocale(Locale.ENGLISH);
int length = 0;
while (scn.hasNextDouble()) {
    length++;
    scn.nextDouble();
scn.close();
...