2017-03-09 83 views
0

我有這個格式有數據的文本文件:創建一個從文本文件-java矩陣

word:filename:wordCount 

我想創建一個有值如下矩陣:

the length of the matrix is same the number of the words in the file 
the width is the number of the files. 

對於例如:

apple:file1:2 
apple:file3:4 
cat:file1:3 
tea:file2:5 
ugly:file4:3 

長度= 5 寬度= 4

我想這樣的輸出:

apple:[2,0,4,0] 
cat:[3,0,0,0] 
tea:[0,5,0,0] 
ugle:[0,0,0,3] 

我嘗試讀取該文本文件,然後我分裂由線「:」

String[] keys = line1.split(":"); 

然後創建一個兩個dimentonal陣列像這樣:

String s[][]=new String[4][4];//the 4=the # of words, 4=number of files 

我添加了4,因爲我不知道怎麼去多少個文件從文件

for (int i = 0; i < 4; i++) { 
    for (int j = 0; j < 4; j++) { 
     String h=keys[2];//the word count 
     s[i][j]=h; 
    } 
} 
System.out.println(Arrays.deepToString(s)) 

我得到的輸出是:

[[2, 2, 2], [2, 2, 2], [2, 2, 2]] 
[[4, 4, 4], [4, 4, 4], [4, 4, 4]] 
[[3, 3, 3], [3, 3, 3], [3, 3, 3]] 
[[5, 5, 5], [5, 5, 5], [5, 5, 5]] 
[[3, 3, 3], [3, 3, 3], [3, 3, 3]] 

的任何幫助,請:)

+0

你知道輸入文件中會有多少文字和文件嗎?因爲這將成爲爲您的問題編寫解決方案的關鍵。 –

+0

歡迎來到Stack Overflow!它看起來像你需要學習使用調試器。請幫助一些[互補調試技術](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。如果您之後仍然有問題,請隨時返回更多詳情。 –

+0

仍然存在一個問題,即 - 在遍歷所有單詞時,如何知道哪個單詞與特定索引相關?例如,「蘋果」一詞映射到索引0,單詞「貓」映射到索引1等等。沒有這樣的映射,你不能將wordCount存儲在矩陣中的特定位置,不是嗎? –

回答

0

從您首選的輸出,它看起來像你想在地圖上標註整數數組的

這是我會做

package q42705914; 

import java.io.BufferedReader; 
import java.io.FileReader; 
import java.io.IOException; 
import java.util.Arrays; 
import java.util.HashMap; 
import java.util.Map; 
import java.util.Map.Entry; 

public class Counter { 
    private static final int MAX_FILES = 4; 
    private final Map<String, int[]> counts = new HashMap<>(); 

    public Counter() { 
     countAll("rawCounts.txt"); 
     print(); 
    } 

    private void countAll(String filename) { 
     try(FileReader fileReader = new FileReader(filename); 
     BufferedReader bufferedReader = new BufferedReader(fileReader)) { 
      String line; 
      while ((line = bufferedReader.readLine()) != null) { 
       countLine(line); 
      } 
     } catch (IOException e) { 
      throw new RuntimeException(e); 
     } 
    } 

    private void countLine(String line) { 
     String[] parts = line.split(":"); 

     if(!counts.containsKey(parts[0])) { 
      counts.put(parts[0], new int[MAX_FILES]); 
     } 
     int[] count = counts.get(parts[0]); 

     int filenumber = Integer.valueOf(parts[1].substring(4)); 

     count[filenumber-1] = count[filenumber-1] + Integer.valueOf(parts[2]); 
    } 

    private void print() { 
     for(Entry<String, int[]> e : counts.entrySet()) { 
      System.out.println(e.getKey() + ":" + Arrays.toString(e.getValue())); 
     } 
    } 

    public static void main(String[] args) throws IOException { 
     new Counter(); 
    } 
} 
+2

堆棧溢出是一個問答網站,而不是作業寫作服務。請不要讓用戶有理由相信。謝謝。 –

+0

@mike非常感謝Mike,我使用了一些修改後的代碼,這很有用。 – user5532529

0

你的代碼中有幾個問題,我是d如果這對你有幫助的話

  1. 至於你提到的key.length = # of words,而不讀取整個文件,你怎麼知道有多少獨特的詞有哪些?你沒有在你的問題中提到這個問題,這使你的問題變得模糊!

  2. 更重要的是,String[] keys = line1.split(":");應該給你key.length = 3,因爲你的輸入格式是word:filename:wordCount

  3. 在內循環中,for (int j=0; j<4; j++) - 爲什麼迭代四次?你應該迭代3次,對吧?

所以,這個想法應該是 - 要在文件中的每一行,然後通過每個項目由:隔開一行迭代。

嘗試邏輯思考你在編寫程序時正在做什麼!

+0

嗨瓦斯,對第一點來說,正確的是與key.length不相同的字數,我把它從問題中刪除。對於第3點,我必須迭代3,因爲循環以零開始。 – user5532529