2013-11-20 34 views
0

我正在尋找對java中所有小寫字符.txt文件執行字符計數,目前打印方法在嘗試打印計數時最終爲空。任何幫助將不勝感激。文本文件的字符數

public static void main(String[] args) throws IOException { 
    File file1 = new File("newfile.txt"); 
    BufferedReader in = new BufferedReader (new FileReader (file1)); 
    System.out.println("Relative Letter Frequency"); 

    int ch; 
    char i = 0; 
    // Declare 26 char counting 
    int[] count = new int[26]; 
    //Loop through the file char 
    while ((ch = in.read()) != -1) { 
    i = (char) ch; 
     if (i >= 'a' && i <= 'z') 
     count[i - 'a']++; 
    } 

    //Print loop 
    for (int j = 0; j < 26; j++) { 
     //Print formatting so each letter is represented on 
     //a new line with correct spacing 
     System.out.printf("%c %d \n", j + 'a', count[j]); 
     System.out.println(); 
    } 

    in.close(); 
} 

int Counter(File file,String Charset) throws IOException { 
     File file1 = new File("newfile.txt"); 
     BufferedReader in = new BufferedReader(new FileReader(file1)); 

     int Count = 0; 
     while (in.read() > -1){ 
      Count++; 

     } 
     System.out.println(Count); 
     in.close();  
     return Count; 
    } 
} 

我試圖更改字符計數器的循環,但依然無果

  public void Counter(File file,String Charset) throws IOException { 
     File file1 = new File("newfile.txt"); 
     BufferedReader in = new BufferedReader(new FileReader(file1)); 
     int CH; 
     char k = 0; 
     int [] Count = new int [1]; 
     while ((CH = in.read()) != -1){ 
      k = (char) CH; 
      if (k >= 'a' && k <='z') 
      Count [k]++;  
     } 
    System.out.println(Count[k]); 
     in.close(); 
    } 
+0

你可以通過字符的字節大小來劃分文件大小嗎? – Discipol

+0

對我來說工作得很好,究竟是什麼問題? @Discipol'in.read()'讀取字節,所以它應該分開;} – Ishtar

+0

@Discipol如果將文件大小除以字符的字節大小,這也包括空格和新行標記。 – user3008724

回答

0

我知道這聽起來令人難以置信的愚蠢,但是爲什麼不直接使用文件的長度。 ToString方法 你想跳過空格和回車或什麼的? 只是尋找你在這裏試圖做的一些澄清......

或者只是循環遍歷每個SubString並創建一個函數返回如果它是「A」或「a」等.. 我是不是C編碼器,但我用做一些C++的工作的話,我會盡量在我理解的語言勾勒出它你...

Public Function GetIsCharacter(ByVal sCharacter As String) As Boolean 

If sCharacter = "A" Then ' etc... 
    Return True 
End If 

' else... 
Return False 

End Function 
2

因爲我慢慢體會你需要什麼,我有調整您的代碼以顯示如何執行操作的兩種方法。

這是一個讀取文件兩次的示例。第一次使用BufferedReader逐行讀取字符。

直接在下面的第二個解決方案按字節讀取填充字節並計數字符,因爲您可能會嘗試這樣做。

package snippet; 

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileReader; 
import java.io.IOException; 
import java.io.InputStream; 

public class Snippet { 
    public static void main(String[] args) throws IOException { 
    System.out.println("Sum: " + counter(new File("newfile.txt"))); 
    } 

    public static int counter(File file) throws IOException { 
    BufferedReader in = new BufferedReader(new FileReader(file)); 
    String line; 
    int[] counter = new int[26]; 
    while ((line = in.readLine()) != null) { 
     for (char c : line.toCharArray()) { 
     if (c >= 'a' && c <= 'z') { 
      counter[c - 'a']++; 
     } 
     } 
    } 

    InputStream is = new FileInputStream(file); 
    int read; 
    while ((read = is.read()) != -1) { 
     if ((read >= 'a') && (read <= 'z')) { 
     counter[read - 'a']++; 
     } 
    } 

    int result = 0; 
    for (int count : counter) { 
     System.out.println(count); 
     result += count; 
    } 
    return result; 
    } 
}