2014-10-11 57 views
0

我被給了一個冬季奧運會活動的文本文件。 它包含團隊,競爭對手的名字和得分。添加/排序與圖形

FRAMae Berenice MEITE   455.455 
CHNKexin ZHANG    454.584 
UKRNatalia POPOVA    453.443 
GERNathalie WEINZIERL   452.162 
RUSEvgeny PLYUSHCHENKO  191.399 
CANPatrick CHAN    189.718 
CHNHan YAN     185.527 
CHNCheng & Hao    271.018 
ITAStefania & Ondrej   270.317 
USAMarissa & Simon   264.256 
GERMaylin & Daniel   260.825 
FRAFlorent AMODIO    179.936 
GERPeter LIEBERS    179.615 
ect.... 

唯一的數字是數字中的最後一位數字。 對於每個球隊,我需要將他們的總分數加起來並顯示前5名球隊。

到目前爲止的代碼:

public class project2 { 

public static void main(String[] args) throws IOException { 
    // TODO Auto-generated method stub 

    String[] array = new String[41]; 
    String[] info = new String [41]; 
    String[] stats = new String[41];  
    String[] team = new String[41]; 

      //.txt file location 
      FileInput fileIn = new FileInput(); 
      fileIn.openFile("C:\\Users\\O\\Desktop\\turn in\\team.txt"); 

      int i=0; 
      String line = fileIn.readLine(); 
      array[i] = line; i++; 
      while (line != null) { 

       line = fileIn.readLine(); 
       array[i] = line; i++; 
      } 





      //Splitting up Info/ Score into two arrays 

      for (int j =0; j< 40; j++){ 
       team[j] = array[j].substring (0, 3).trim(); 
       info[j] = array[j].substring (3, 30).trim(); 
       stats[j] = array[j].substring (36).trim(); 
      } 



      double[] statsDub = new double[41]; 
      for (int k =1; k < 40; k++){ 
      statsDub[k] = Double.parseDouble(stats[k]); 
      } 
      Map<String,Double> totalScore = new HashMap<>(); 
      for (int j =0; j< 40; j++){ 
       totalScore.put(team[j], statsDub[j]); 
      } 
      // Get a set of the entries 
       Set set = totalScore.entrySet(); 
       // Get an iterator 
       Iterator i1 = set.iterator(); 
       // Display elements 
       while(i1.hasNext()) { 
       Map.Entry me = (Map.Entry)i1.next(); 
       System.out.print(me.getKey() + ": "); 
       System.out.println(me.getValue()); 
       } 

打印出:

GER: 5.0 
USA: 7.0 
ITA: 6.0 
RUS: 8.0 
CHN: 1.0 
JPN: 8.0 
FRA: 7.0 
CAN: 6.0 
UKR: 2.0 
GBR: 1.0 

這是打印出來只有一個每隊得分。關於如何總結每個團隊的分數的建議?

謝謝!

回答

0

這裏如果修訂,嘗試:

Map<String,Double> totalScore = new HashMap<>(); 
     for (int j =0; j< 40; j++){ 
    Double tmp = totalScore.get (team[j]); 
    if (tmp != null) 
    { 
     totalScore.put(team[j], statsDub[j]+tmp); 
    } 
    else 
      totalScore.put(team[j], statsDub[j]); 
     } 

這一行:totalScore.put(團隊[J],statsDub [J]);只是將值添加不加,因此每個鍵只有最後一個分數。

這裏是與代碼工作示例我已經張貼以上:

package pkg; 


import java.io.*; 
import java.util.*; 


public class pkg 
{ 
public static void main(String[] args) throws IOException { 
    // TODO Auto-generated method stub 

    String[] array = { 
      "FRAMae Berenice MEITE   455.455", 
      "CHNKexin ZHANG    454.584", 
      "UKRNatalia POPOVA    453.443", 
      "GERNathalie WEINZIERL   452.162", 
      "RUSEvgeny PLYUSHCHENKO  191.399", 
      "CANPatrick CHAN    189.718", 
      "CHNHan YAN     185.527", 
      "CHNCheng & Hao    271.018", 
      "ITAStefania & Ondrej   270.317", 
      "USAMarissa & Simon   264.256", 
      "GERMaylin & Daniel   260.825", 
      "FRAFlorent AMODIO    179.936", 
      "GERPeter LIEBERS    179.615"}; 
    String[] info = new String [13]; 
    String[] stats = new String[13];  
    String[] team = new String[13]; 




      //Splitting up Info/ Score into two arrays 

      for (int j =0; j< 13; j++){ 
       team[j] = array[j].substring (0, 3).trim(); 
       info[j] = array[j].substring (3, 30).trim(); 
       stats[j] = array[j].substring (36).trim(); 
      } 



      double[] statsDub = new double[13]; 
      for (int k =1; k < 13; k++){ 
      statsDub[k] = Double.parseDouble(stats[k]); 
      } 
      Map<String,Double> totalScore = new HashMap<>(); 
      for (int j =0; j< 13; j++){ 

        Double tmp = totalScore.get (team[j]); 
        if (tmp != null) 
        { 
         totalScore.put(team[j], statsDub[j]+tmp); 
        } 
        else 
          totalScore.put(team[j], statsDub[j]); 




      } 
      // Get a set of the entries 
       Set set = totalScore.entrySet(); 
       // Get an iterator 
       Iterator i1 = set.iterator(); 
       // Display elements 
       while(i1.hasNext()) { 
       Map.Entry me = (Map.Entry)i1.next(); 
       System.out.print(me.getKey() + ": "); 
       System.out.println(me.getValue()); 
       } 
} 
} 

輸出:

RUS:9.0 CAN:8.0 USA:6.0 FRA:6.0 GER:12.0 ITA:7.0 CHN:19.0 UKR:3.0

+0

感謝您的回覆,但我收到錯誤「The operator!= is undefined for the argume nt類型double,null「代碼的第4行 – obsi 2014-10-11 16:17:24

+0

我編輯了答案,將double更改爲Double。我有一個小錯字。 – 2014-10-11 16:24:43

+0

我不相信我沒有注意到這個錯字。但它似乎沒有正常工作。它只是爲每個團隊打印0。我會嘗試調試它,但如果您有任何建議,請讓我知道。 – obsi 2014-10-11 16:31:49