【问题标题】:Scanning in contents of a file扫描文件内容
【发布时间】:2015-11-28 12:19:18
【问题描述】:

我有一个file,其内容如下:

5

Derrick Rose

1 15 19 26 33 46

Kobe Bryant

17 19 33 34 46 47

Pau Gasol

1 4 9 16 25 36

Kevin Durant

17 19 34 46 47 48

LeBron James

5 10 17 19 34 47

每个名称和数字之间有一个空行。但是,当我使用 nextLine(); 扫描文件时方法我得到以下内容:

NAME: 
NAME: 
NAME: Derrick Rose
NAME: 
NAME: 1 15 19 26 33 46
NAME: 
NAME: Kobe Bryant
NAME: 
NAME: 17 19 33 34 46 47
NAME: 

谁能告诉我问题出现在我的代码中的什么地方以及为什么它在空白行中扫描。

    Scanner scan = new Scanner(file);
    int lim = scan.nextInt();
    for(int i = 0; i < (lim * 2); i++)
    {
        String name = scan.nextLine();
        System.out.println("NAME: " + name);
    }

【问题讨论】:

  • 它正在扫描空行,因为您的输入有空行。你提到一个错误,你能详细说明一下吗?你遇到了什么错误?
  • nextLine() 逐行读取您的文件,包括空行。试试if (name.equals("")) continue; 之类的东西不要打印出来。
  • @Trobbins 抱歉,我刚刚修复了它,它没有出现错误。但我的印象是扫描仪忽略了每行之间的空白行
  • @Beginner 它没有。扫描仪按原样读取输入。

标签: java string file java.util.scanner


【解决方案1】:

下面的 ExcludeEmptyLines.java 类起作用了

    package com.stackoverflow.java.scannertest;

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;

    /**
     * Created by fixworld.net on 03-September-2015.
     */

    public class ExcludeEmptyLines {

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

            String dataFileFullPath = "<replace with full path to input file>";

            File inputFile = new File(dataFileFullPath);

            Scanner scan = new Scanner(inputFile);

            int numEntries = scan.nextInt();

            System.out.println("NumEntries = " + numEntries);

            for(int i = 0; i <= (numEntries * 4); i++)
            {
                String inputLine = scan.nextLine();
                if(!inputLine.trim().equalsIgnoreCase("")) {
                    System.out.println("inputLine = " + inputLine);
                }
            }
        }
    }

运行它的输出是

    NumEntries = 5
    inputLine = Derrick Rose
    inputLine = 1 15 19 26 33 46
    inputLine = Kobe Bryant
    inputLine = 17 19 33 34 46 47
    inputLine = Pau Gasol
    inputLine = 1 4 9 16 25 36
    inputLine = Kevin Durant
    inputLine = 17 19 34 46 47 48
    inputLine = LeBron James
    inputLine = 5 10 17 19 34 47

【讨论】:

    【解决方案2】:

    您似乎想忽略空行,您可以检查您将要写入控制台的行的长度。不可能不扫描空行,您至少需要跳过它们才能进一步深入您的信息流。

    Scanner scan = new Scanner(file);
    int lim = scan.nextInt();
    for(int i = 0; i < (lim * 2); i++) {
        String name = scan.nextLine();
        if (name.trim().length() > 0)
             System.out.println("NAME: " + name);
    }
    

    【讨论】:

      【解决方案3】:

      如果您的输入中有换行符,那么这将消耗一次对scan.NextLine() 的调用,因为该函数以换行符分隔。如果您希望它忽略空白行,那么您应该明确检查它们,然后如果这些行不为空白,则明确增加您的计数器。

      Scanner scan = new Scanner(file);
      int lim = scan.nextInt();
      for(int i = 0; i < (lim * 2); )
      {
          String name = scan.nextLine();
          if (!name.trim().equals("")) i++;
          System.out.println("NAME: " + name);
      }
      

      【讨论】:

      • 别忘了修剪()! :) !name.trim().equals("").
      • @user244255,已修复。谢谢。
      【解决方案4】:

      您可以检查空行并忽略它们。下面的代码应该做到这一点。

      Scanner scan = new Scanner(file);
      int lim = scan.nextInt();
      for(int i = 0; i < (lim * 2); i++)
      {
          String name = scan.nextLine();
          if (!name.equals("")){
              System.out.println("NAME: " + name);
          }
      }
      

      【讨论】:

      • 比较字符串时需要使用.equals()
      猜你喜欢
      • 2021-12-16
      • 2017-08-02
      • 2016-01-10
      • 1970-01-01
      • 1970-01-01
      • 2016-06-17
      • 1970-01-01
      • 2012-03-15
      相关资源
      最近更新 更多