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

StringgetNextLine(final Scanner scanner)
Skips comment lines
while (scanner.hasNextLine()) {
    String line = scanner.nextLine().trim();
    if (line.startsWith("//")) {
        continue; 
    if (line.startsWith("/*")) { // multi line comment
        String skipLine = line;
        while (scanner.hasNextLine() && !skipLine.contains("*/")) { 
...
StringgetNextLine(final Scanner source)
Read lines from input java.util.Scanner until it finds a non-commented line, then send it back.
String line;
while (source.hasNextLine()) {
    line = source.nextLine();
    if (line.startsWith(COMMENT_STRING)) {
        continue;
    } else {
        final int commentIndex = line.indexOf(COMMENT_STRING);
        if (commentIndex < 0) {
...
StringgetNextWord(Scanner scan)
get Next Word
String word = scan.nextLine();
return word.trim();
intgetNextWord(String str, int index, StringBuilder outToken, String ignoreRegex)
Retrieves the next word in str.
if (index >= str.length())
    return str.length();
String rem = str.substring(index, str.length());
Scanner s = new Scanner(rem);
s.useDelimiter(ignoreRegex);
String token = null;
while (s.hasNext() && (token = s.next()).length() == 0)
    ;
...
intgetNumberOfUsernames(String usernames)
get Number Of Usernames
Scanner scanner = new Scanner(usernames);
scanner.useDelimiter(",");
int i = 0;
while (scanner.hasNext()) {
    scanner.next();
    i++;
return i;
...
StringgetOutputFromValgrindOutput(String valgrindOutput)
get Output From Valgrind Output
StringBuilder programOutput = new StringBuilder();
Scanner scanner = new Scanner(valgrindOutput);
while (scanner.hasNextLine()) {
    String line = scanner.nextLine();
    if (!line.startsWith("==")) {
        programOutput.append(line);
        programOutput.append(System.getProperty("line.separator"));
return programOutput.toString();
StringgetPartOfString(String string, int index, String delimiter)
get Part Of String
Scanner scanner = new Scanner(string).useDelimiter(delimiter);
for (int i = 1; i < index; i++) {
    scanner.next();
return scanner.next();
StringgetReplay(String prompt)
get Replay
Scanner scanner = new Scanner(System.in, "UTF-8");
System.out.println(prompt);
String str = scanner.nextLine().trim();
scanner.close();
return str;
StringgetScannerContents(final Scanner scanner)
get Scanner Contents
String content;
if (scanner.hasNext()) { 
    content = scanner.next();
} else {
    content = "";
return content;
String[]getStrings(Properties props, String key, String sep)
Gets an array of string properties given a key and a proper word separator.
String val = props.getProperty(key);
if (val == null || val.trim().equals(""))
    return new String[0];
ArrayList<String> l = new ArrayList<String>(5);
Scanner s = new Scanner(val);
s.useDelimiter(sep);
while (s.hasNext()) {
    String v = s.next();
...