我有一個從文本文件中獲取數據的程序。該文件格式,如Java - 丟失數據,包含字符串和整數的映射
Date/Time Tip From (Name)
Message(If one was left)
(Tipped Amount) (Total Tips Recieved)
我已經能夠把我的代碼,將其分離出來,並把它放在一個地圖,使得它增加了所有每名並輸出遞減的方式提示。
E.g
INPUT ----------------------------------
Dec. 14, 2013, 2:31 a.m. Tip from rs
25 24986
Dec. 14, 2013, 2:27 a.m. Tip from ro
100 24961
Dec. 14, 2013, 2:27 a.m. Tip from rs
15 24861
Dec. 14, 2013, 2:25 a.m. Tip from da
3 24846
OUTPUT-----------------------------------
ro=100
rs=40
da=3
我現在碰到的問題。我發現我正在失去數據,不知道爲什麼。在文本文件中有1000個以上的文字,大約有2000行文字。其中一名學員,手工計算的X值爲1990年。運行時,程序計算的實際計算值只比實際計算的少1690,300。我在試圖調試時發現數據可能被刪除或跳過的地方。
這裏是從我的代碼的摘錄相關的計算器執行
while ((line = bufferedReader.readLine()) != null) {
if (line.contains("Tip from")) { // Finds the line that contains
// the tippers name
final String tipperName = line.substring(line
.indexOf("from ") + 5);
currentTipper = tipperName;
} else if (line.substring(0, 1).matches("\\d")) { // finds the
// line that
// contains
// the
// tipped
// amount
final Integer tipValue = Integer.parseInt(line.substring(0,
line.indexOf("\t")));
// here we store the tip in the map. If we have a record
// we
// sum, else
// we store as is
tipsByName
.put(currentTipper,
(tipsByName.get(currentTipper) == null ? 0
: tipsByName.get(currentTipper))
+ tipValue);
} else { // if line doesnt contain a name or a tip, skips to
// next line
bufferedReader.readLine();
}
}
如果完整的代碼會更有幫助,請讓我知道,我會編輯的帖子。
謝謝!
不要認爲這是問題..但總是修剪()線。 – TheLostMind