ファイルを読み取り、各単語間のスペースを削除するスキャナーを作成しようとしています。私はこれを多く得ることができますが、私は同じ行にとどまるところにそれを得ることはできません。私は、行を読み、空白を削除し、次の行に行くプログラムを得ることができません。これは私の練習プロジェクトからのテキストです: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