2016-05-17 18 views
0

大多數情況下,它可以正常工作。很少有一個計數。任何猜測?爲什麼我的單詞有時會被一個計數器打斷?

public static int countWords(File file) throws FileNotFoundException, IOException{ 
     BufferedReader br = new BufferedReader(new FileReader(file)); 
     String line; 
     List<String> strList = new ArrayList<>(); 

     while ((line=br.readLine())!=null){ 
      String[] strArray= line.split("\\s+"); 
      for (int i=0; i<strArray.length;i++){ 
       strList.add(strArray[i]); 
      } 
     } 
     return strList.size(); 

    } 

特別是在下面的例子中它給3而不是2:

\n 
      k 
+0

你認爲'\ n'是一個單詞嗎?在你的例子中,我會認爲'k'是唯一的*單詞*。 –

+2

我想它是計數新行爲1,選項卡爲第二,然後k爲第三;) –

+0

我該如何解決它? @BilboBaggins –

回答

1

如果您正在使用的Java 8,你可以使用流和過濾你認爲是一個「字」。例如:

List<String> l = Files.lines(Paths.get("files/input.txt")) // Read all lines of your input text 
      .flatMap(s->Stream.of(s.split("\\s+"))) // Split each line by white spaces 
      .filter(s->s.matches("\\w")) // Keep only the "words" (you can change here as you want) 
      .collect(Collectors.toList()); // Put the stream in a List 

在這種情況下,它將輸出[k]

當然你也可以做同樣的的Java 7通過調整你的代碼,並在你的for循環添加此條件:

if(strArray[i].matches("\\w")) 
    strList.add(strArray[i]); // Keep only the "words" - again, use your own criteria 

這只是比較繁瑣。

我希望它有幫助。

+0

爲什麼你將流拖到這樣簡單的問題? – Nikem

+0

該文件是一條線的流,線是單詞的流。不需要使用'BufferedReader'和'FileReader'或做顯式循環。結果更短,更具可讀性。 – joel314

+0

但你把整個文件讀入內存。如果文件很大,逐行讀取會更好。你的代碼會消耗更多的內存。 – Nikem

2

我想第二行被分成兩個字符串「」和「k」。請參閱下面的代碼:

import java.util.Arrays; 

public static void main(String[] args) { 
    String str = "   k"; 
    String[] array = str.split("\\\s+"); 
    System.out.println("length of array is " + array.length); // length is 2 
    System.out.println(Arrays.toString(array)); //array is [, k] 
} 
相關問題