我试图让扫描仪读取文件并删除每个单词之间的空格。我可以得到这么多,但我不能把它放在他们留在同一条线上的地方。我无法让程序读取一行,删除空格,然后转到下一行。这是从我的实践项目中显示:Java使用扫描仪读取文件,然后读取线
four score and seven years ago our fathers brought forth on this continent a new nation
我目前只获得了第一线
,这是我的代码:
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() ;”没有被使用,但我不知道如何去使它循环到文本中的下一行......它在第一行运行,然后什么也没有 – user8686545