Java Scanner Usage getNextLine(final Scanner scanner)

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

Description

Skips comment lines

License

Open Source License

Parameter

Parameter Description
scanner a parameter

Return

comment free line

Declaration

public static String getNextLine(final Scanner scanner) 

Method Source Code

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

import java.util.Scanner;

public class Main {
    /**/* ww  w. j  av  a 2s .  c o  m*/
     * Skips comment lines
     * 
     * @param scanner
     * @return comment free line
     */
    public static String getNextLine(final Scanner scanner) {
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine().trim();
            if (line.startsWith("//")) {
                continue; // Its single comment line, skip
            }

            if (line.startsWith("/*")) { // multi line comment
                String skipLine = line;
                while (scanner.hasNextLine() && !skipLine.contains("*/")) { // skip until close tag
                    skipLine = scanner.nextLine();
                }
                // in case there is data after comment close tag
                line = skipLine.substring(skipLine.indexOf("*/") + 2);
                if (line.isEmpty()) {
                    continue;
                }
            }
            return line;
        }
        return "";
    }
}

Related

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