简体   繁体   English

如何跳过用扫描仪读取一行

[英]How to skip reading a line with scanner

I have read in a text file and am scanning said file. 我已阅读文本文件并正在扫描该文件。 The question I have is how would I skip over lines that include a certain character (in my case lines that start with " // " and " " (whitespace). 我的问题是如何跳过包含某个字符的行(在我的例子中以“//”和“”(空格)开头的行。

Here is my code at the moment. 这是我目前的代码。 Can someone point me in the right direction? 有人能指出我正确的方向吗?

    File dataFile = new File(filename);
    Scanner scanner = new Scanner(dataFile);


      while(scanner.hasNext())
      {
         String lineOfText = scanner.nextLine();
         if (lineOfText.startsWith("//")) {
           System.out.println(); // not sure what to put here
         }
      System.out.println(lineOfText);
      }
   scanner.close();

You will only want to execute the code within the while-loop if the line of text doesn't start with a / or whitespace. 如果文本行不以/或空格开头,您只需要在while循环中执行代码。 You can filter these out as seen below: 你可以过滤掉这些,如下所示:

while(scanner.hasNext()) {
   String lineOfText = scanner.nextLine();
   if (lineOfText.startsWith("//") || lineOfText.startsWith(" ")) {
      continue; //Exit this iteration if line starts with space or /
   }
   System.out.println(lineOfText);
}

As you are iterating over the lines of text in the file, use String 's startsWith() method to check if the line starts with the sequences you are trying to avoid. 在迭代文件中的文本行时,使用StringstartsWith()方法检查行是否以您要避免的序列开头。

If it does, continue to the next line. 如果是,请继续下一行。 Otherwise, print it. 否则,打印出来。

while (scanner.hasNext()) {
    String lineOfText = scanner.nextLine();

    if (lineOfText.startsWith("//") || lineOfText.startsWith(" ") ) {
        continue;
    }

    System.out.println(lineOfText);
}

Just use a continue like - 只需使用continue喜欢 -

if (lineOfText.startsWith("//")) {
  continue; //would skip the loop to next iteration from here
}

Detials - What is the "continue" keyword and how does it work in Java? Detials - 什么是“continue”关键字以及它如何在Java中运行?

If you're just interested in printing out the lines of code that begin with a "//" then you should just use the continue keyword in java. 如果您只想打印出以“//”开头的代码行,那么您应该在java中使用continue关键字。

String lineOfText = scanner.nextLine();
if (lineOfText.startsWith("//")) {
    continue;
}

See this post for more information regarding the "continue" keyword. 有关“继续”关键字的详细信息,请参阅此帖子

You can just insert "else" in your code like: 您可以在代码中插入“else”,如:

public static void main(String[] args) throws FileNotFoundException { public static void main(String [] args)抛出FileNotFoundException {

     File dataFile = new File("testfile.txt");
        Scanner scanner = new Scanner(dataFile);


          while(scanner.hasNext())
          {
             String lineOfText = scanner.nextLine();
             if (lineOfText.startsWith("//")) {
               System.out.println(); 
             }
             else
                 System.out.println(lineOfText);
          }
       scanner.close();
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM