【问题标题】:Java Using Scanner to Read File and Then Read LineJava 使用 Scanner 读取文件然后读取行
【发布时间】:2017-10-19 19:12:07
【问题描述】:

我正在尝试制作一个扫描仪来读取文件并删除每个单词之间的空格。我能得到这么多,但我不能把它带到他们在同一条线上的地方。我无法让程序读取一行,删除空格,然后转到下一行。这是我的练习项目中的文字:

四分和 七年前我们的 父亲带来 在这片大陆 一个新的 国家

我目前只得到第一行

这是我的代码:

import java.util.*;
import java.io.*;
public class CollapseSpace {

    public static void main (String[] args) throws FileNotFoundException{
        Scanner fileInput = new Scanner(new File ("textwithspaces.txt"));
        String nextLine = fileInput.nextLine();
        Scanner lineInput = new Scanner(nextLine);
        while(fileInput.hasNext()){
            nextLine = fileInput.nextLine();
            while(lineInput.hasNext()){
                System.out.print(lineInput.next() + " "); // I tried to add a fileInput.NextLine() to consume the line but it isn't working properly            
            }
            System.out.println();
       }

    }
}

【问题讨论】:

  • 我知道“nextLine = fileInput.nextLine();”这一行没有被使用,但我不知道如何让它循环到文本中的下一行......它在第一行运行,然后什么都没有

标签: java java.util.scanner


【解决方案1】:

如果您只需要逐行迭代并删除单词之间的空格,那么您只需要一个循环,下面的示例代码应该可以解决问题

public static void main (String[] args) throws FileNotFoundException{
    final Scanner fileInput = new Scanner(new File ("src/main/resources/textwithspaces.txt"));
    while(fileInput.hasNext()){
        final String nextLine = fileInput.nextLine();
        // remove all spaces
        final String lineWithOutSpaces = nextLine.replaceAll("\\s+","");
        System.out.println(lineWithOutSpaces);
    }
}

【讨论】:

    【解决方案2】:

    首先,您不应该使用* 来导入类。它通常被认为是“不好的做法”,因为它会干扰你自己的课程,而且它不是很明确。

    您需要在自己的循环中循环 nextLine 方法。并且还使用字符串的 replaceAll 方法会很好。

    我在下面展示了一个示例:

    import java.util.Scanner;
    import java.io.File;
    import java.io.FileNotFoundException;
    
    class Main {
        public static void main(String[] args) throws FileNotFoundException {
    
            // Create an object to represent a text file
            File file = new File("textwithspaces.txt");
    
            // Create a scanner with the text file as argument
            Scanner scannerWithFile = new Scanner(file);
    
            // Continue as long as it has a next line
            do {
                // Replace strings
                String thisLine = scannerWithFile.nextLine();
    
                // Only print the line out if not empty
                if (!thisLine.isEmpty()) {
                    // Replace all spaces
                    thisLine = thisLine.replaceAll(" ", "");
    
                    // Print
                    System.out.println(thisLine);
                }
            } while (scannerWithFile.hasNext());
        }
    }
    

    我还将您的 while 循环切换为 do while 循环,这样您就可以立即进入循环而无需先检查条件,它在下一次迭代之前完成。

    【讨论】:

      【解决方案3】:

      您最大的问题是您在循环之外声明了nextLine = fileInput.nextLine();,然后在Scanner lineInput = new Scanner(nextLine); 中使用了它,因此它成为了文本的第一行,但之后永远不会改变。

      我也同意另一条评论说你不应该使用*,这样广泛地导入被认为是不好的做法,因为你要导入很多你不会使用的东西。

      我重构了您的代码以使其正常工作。

      import java.util.Scanner;
      import java.io.File;
      import java.io.FileNotFoundException;
      
      public class Main {
      
          public static void main (String[] args) throws FileNotFoundException{
              Scanner fileInput = new Scanner(new File ("textwithspaces.txt"));
      
              while(fileInput.hasNext()){
                  String nextLine = fileInput.nextLine();
                  Scanner lineInput = new Scanner(nextLine);
      
                  while(lineInput.hasNext()){
                      System.out.print(lineInput.next() + " ");            
                  }
                 System.out.println();
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2017-01-18
        • 2012-12-02
        • 2023-03-15
        • 1970-01-01
        • 2021-01-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多