2017-10-19 162 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循環到一個do 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); 
    } 
}