2016-11-22 24 views
0

私はハフマンプロジェクトに取り組んでいます。私は基本的な作業をしています。指定された文字列の文字をカウントして表示します。しかし、私はファイルから手動で文字列を出力するのではなく、ファイルから読み取るようにしたい。このハフマンプロジェクトをファイルから読み込むにはどうすればよいですか?

私のコードは以下の通りですが、テキストファイルの内容を印刷するためのアドバイスは非常に便利です。

import java.util.ArrayList; 

public class Main { 

public static void main(String[] args) { 

    String message = "Hello"; 

    // Convert the string to char array 

    char[] msgChar = message.toCharArray(); 
    ArrayList<Character> characters = new ArrayList<Character>(); 

    /* 
    * Get a List of all the chars which are present in the string No 
    * repeating the characters! 
    */ 
    for (int i = 0; i < msgChar.length; i++) { 
     if (!(characters.contains(msgChar[i]))) { 
      characters.add(msgChar[i]); 
     } 
    } 

    System.out.println(message); 
    System.out.println(""); 

    /* Count the number of occurrences of Characters */ 
    int[] countOfChar = new int[characters.size()]; 

    /* Fill The Array Of Counts with one as base value */ 
    for (int x = 0; x < countOfChar.length; x++) { 
     countOfChar[x] = 0; 
    } 

    /* Do Actual Counting! */ 
    for (int i = 0; i < characters.size(); i++) { 
     char checker = characters.get(i); 
     for (int x = 0; x < msgChar.length; x++) { 
      if (checker == msgChar[x]) { 
       countOfChar[i]++; 
      } 
     } 
    } 

    /* Sort the arrays is descending order */ 
    for (int i = 0; i < countOfChar.length - 1; i++) { 
     for (int j = 0; j < countOfChar.length - 1; j++) { 
      if (countOfChar[j] < countOfChar[j + 1]) { 
       int temp = countOfChar[j]; 
       countOfChar[j] = countOfChar[j + 1]; 
       countOfChar[j + 1] = temp; 

       char tempChar = characters.get(j); 
       characters.set(j, characters.get(j + 1)); 
       characters.set(j + 1, tempChar); 
      } 
     } 
    } 

    /* Print Out The Frequencies of the Characters */ 
    for (int x = 0; x < countOfChar.length; x++) { 
     System.out.println(characters.get(x) + " - " + countOfChar[x]); 
    } 

} 
} 
+0

[ファイルの内容からJava文字列を作成するにはどうすればよいですか?](http://stackoverflow.com/questions/326390/how-do-i-create-a-java-string-from-ファイルの内容(contents-of-a-file) – Slartibartfast

答えて

0
String line; 
try (
    InputStream fis = new FileInputStream("file_name"); 
    InputStreamReader isr = new InputStreamReader(fis); 
    BufferedReader br = new BufferedReader(isr); 
) { 
while ((line = br.readLine()) != null) { 
    // Deal with the line 
} 
} 

きれいな方法は、私はあなたが使用しているバージョンのJava
List<String> lines = Files.readAllLines(path);

0

を知らないされるように、この方法では解決されます:

private static String readFromFile(String filename) { 
    try { 
     StringBuilder sb = new StringBuilder(); 
     BufferedReader reader = new BufferedReader(new FileReader(filename)); 
     String line; 
     while ((line = reader.readLine()) != null) 
      sb.append(line).append("\n"); 
     reader.close(); 
     return sb.toString(); 
    } catch (IOException e) { 
     System.err.println("File " + filename + " cannot be read: " + e.getMessage()); 
     return null; 
    } 
} 

そして中をあなたのコードは、次のように使用できます:

public static void main(String[] args) { 
    String message = readString("testfile.txt"); 
    if(message == null) 
     return; // cannot read! 
関連する問題