2013-12-16 74 views
0

我有一個從文本文件中獲取數據的程序。該文件格式,如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(); 

      } 
     } 

如果完整的代碼會更有幫助,請讓我知道,我會編輯的帖子。

謝謝!

+0

不要認爲這是問題..但總是修剪()線。 – TheLostMind

回答

3

我認爲你有重複的條目,這就是爲什麼你'失去了數據'。在向地圖插入條目之前,請嘗試查看密鑰是否已經存在。

+0

,同時也要記住,一個地圖搜索相同的條目,而不是相同的(所以'String a =「tip7」'和'String b =「tip7」'將會相互覆蓋,儘管這些可能是兩個不同的對象) – MyPasswordIsLasercats

+0

建議使用地圖<字符串,列表>將自動翻譯器映射到多個提示。 – Stroboskop

+0

@Stroboskop是的,這是一條路。 – piokuc

0

你可以使用正則表達式來解析完整的文件(Java 7中):

import java.io.IOException; 
import java.nio.ByteBuffer; 
import java.nio.charset.Charset; 
import java.nio.file.Files; 
import java.nio.file.Paths; 
import java.util.*; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

public class Test { 

    public static <K, V extends Comparable<? super V>> Map<K, V> 
    sortByValueDescending(Map<K, V> map) 
    { 
     List<Map.Entry<K, V>> list = 
       new LinkedList<Map.Entry<K, V>>(map.entrySet()); 
     Collections.sort(list, new Comparator<Map.Entry<K, V>>() 
     { 
      public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) 
      { 
       return (o2.getValue()).compareTo(o1.getValue()); 
      } 
     }); 

     Map<K, V> result = new LinkedHashMap<K, V>(); 
     for (Map.Entry<K, V> entry : list) 
     { 
      result.put(entry.getKey(), entry.getValue()); 
     } 
     return result; 
    } 

    static String readFile(String path, Charset encoding) 
      throws IOException 
    { 
     byte[] encoded = Files.readAllBytes(Paths.get(path)); 
     return encoding.decode(ByteBuffer.wrap(encoded)).toString(); 
    } 

    public static void main(String[] args) throws IOException 
    { 
     //String sourceString = readFile(args[1], Charset.defaultCharset()); 
     String sourceString = "Dec. 14, 2013, 2:31 a.m.  Tip from rs\n" + 
       "25 24986\n" + 
       "Dec. 14, 2013, 2:27 a.m.  Tip from ro\n" + 
       "100 24961\n" + 
       "Dec. 14, 2013, 2:27 a.m.  Tip from rs\n" + 
       "15 24861\n" + 
       "Dec. 14, 2013, 2:25 a.m.  Tip from da\n" + 
       "3 24846"; 

     Pattern re = Pattern.compile("^[\\w]{3}\\.\\s\\d{1,2},\\s\\d{4},\\s\\d{1,2}:\\d{2}\\s[ap]\\.m\\.\\s+Tip\\sfrom\\s(\\w+)\\s*^(\\d+)\\s+\\d+\\s*" 
       ,Pattern.CASE_INSENSITIVE | Pattern.MULTILINE); 

     Map<String,Integer> tips = new HashMap<String, Integer>(); 

     Matcher m = re.matcher(sourceString); 
     while (m.find()){ 
      String server = m.group(1); 
      Integer tip = Integer.parseInt(m.group(2)); 

      Integer serverTips = tips.get(server); 
      if(serverTips == null) serverTips = 0; 
      serverTips += tip; 

      tips.put(server, serverTips); 
     } 

     Map<String,Integer> sortedTips = sortByValueDescending(tips); 
     for(Map.Entry<String,Integer> entry : sortedTips.entrySet()) 
     { 
      System.out.println(entry.getKey()+"="+ entry.getValue()); 
     } 
    } 
} 

輸出:

ro=100 
rs=40 
da=3 

可以與路徑的文件替換args[1]和更改編碼如果你不使用默認編碼。

+0

我試過了。我仍然遇到丟失數據的問題:(我會張貼整個txt文檔,但我認爲StackOverflow可能會殺了我哈哈2500行左右的文本 – user3079061