2017-10-19 3 views
0

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

    } 
} 
+0

を行う必要があり、私はラインを知っている「nextLine = fileInput.nextLine() ; "使用されていませんが、テキストの次の行にループを引かせる方法についてはわかりません...最初の行で実行してから何も実行しません – user8686545

答えて

-1

あなたの最大の問題は、あなたが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(); 
     } 
    } 
} 
0

まず、*を使用してクラスをインポートしないでください。それはあなた自身のクラスに干渉する可能性があるため、一般的に「悪い練習」と考えられています。それはあまり明確ではありません。

独自のループ内で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ループを切り替え、それは次の反復の前に行われます。

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); 
    } 
} 
関連する問題