Java Scanner Usage getNextLine(final Scanner source)

Here you can find the source of getNextLine(final Scanner source)

Description

Read lines from input java.util.Scanner until it finds a non-commented line, then send it back.

License

Open Source License

Parameter

Parameter Description
source The scanner to read lines from.

Return

The first non-commented line found, or null if scanner is empty or contains only comments.

Declaration

static String getNextLine(final Scanner source) 

Method Source Code

//package com.java2s;
/*//from  w w w. j  a v a2  s .  com
 *    Geotoolkit - An Open Source Java GIS Toolkit
 *    http://www.geotoolkit.org
 *
 *    (C) 2010-2015, Geomatys
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation;
 *    version 2.1 of the License.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 */

import java.util.Scanner;

public class Main {
    public static final String COMMENT_STRING = "#";

    /**
     * Read lines from input {@link java.util.Scanner} until it finds a non-commented line, then send it back.
     * @param source The scanner to read lines from.
     * @return The first non-commented line found, or null if scanner is empty or contains only comments.
     */
    static String getNextLine(final Scanner source) {
        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) {
                    return line;
                } else {
                    return line.substring(0, commentIndex);
                }
            }
        }
        return null;
    }
}

Related

  1. getLines(String str)
  2. getLineStartingWith(Scanner scanner, String starts)
  3. getLngIn()
  4. getNextInput(Scanner scan)
  5. getNextLine(final Scanner scanner)
  6. getNextWord(Scanner scan)
  7. getNextWord(String str, int index, StringBuilder outToken, String ignoreRegex)
  8. getNumberOfUsernames(String usernames)
  9. getOutputFromValgrindOutput(String valgrindOutput)