我有這個格式有數據的文本文件:創建一個從文本文件-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]]
的任何幫助,請:)
你知道輸入文件中會有多少文字和文件嗎?因爲這將成爲爲您的問題編寫解決方案的關鍵。 –
歡迎來到Stack Overflow!它看起來像你需要學習使用調試器。請幫助一些[互補調試技術](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。如果您之後仍然有問題,請隨時返回更多詳情。 –
仍然存在一個問題,即 - 在遍歷所有單詞時,如何知道哪個單詞與特定索引相關?例如,「蘋果」一詞映射到索引0,單詞「貓」映射到索引1等等。沒有這樣的映射,你不能將wordCount存儲在矩陣中的特定位置,不是嗎? –