Java 树映射集合问题

Java 树映射集合问题,java,treemap,treeset,Java,Treemap,Treeset,我正试图创建一个程序来跟踪大学队和他们的运动记录(赢、输、打了几年)。我想使用一个映射(我选择了TreeMap)和一个集合(我选择了TreeSet)。我想从文件中读取团队,并根据输入更新数据库。例如,1975年:加州大学洛杉矶分校:布朗将增加加州大学洛杉矶分校的胜利,增加布朗的损失,并将1975年添加到两支球队中 树形图的关键是大学名称。这些值是一个名为TeamInfo的对象 import java.util.TreeSet; class TeamInfo { int wins = 0

我正试图创建一个程序来跟踪大学队和他们的运动记录(赢、输、打了几年)。我想使用一个映射(我选择了TreeMap)和一个集合(我选择了TreeSet)。我想从文件中读取团队,并根据输入更新数据库。例如,1975年:加州大学洛杉矶分校:布朗将增加加州大学洛杉矶分校的胜利,增加布朗的损失,并将1975年添加到两支球队中

树形图的关键是大学名称。这些值是一个名为TeamInfo的对象

import java.util.TreeSet;

class TeamInfo {
    int wins = 0;
    int losses = 0;
    TreeSet years;

    TeamInfo(int wins, int losses, String year) {
        // provide constructor code here
        this.wins = wins;
        this.losses = losses;
        years = new TreeSet();
    }

    void incrementWins() {
        wins++;
    }

    void incrementLosses() {
        losses++;
    }

    void addYear(String aYear) {
        // provide addYear code here
        years.add(aYear);
    }

    public String toString() {
        // provide toString() code here
        return " Wins: " + wins + " Losses: " + losses + "\n" + "Years: " +
                years.toString();
    }
} // end TeamInfo
理想情况下,从文件读取后,我应该得到输出:

名称:加州大学洛杉矶分校赢:1输:0

姓名:布朗赢:0输:1

当我有这样的输入时,我的问题就出现了:

1939年:维拉诺瓦:布朗

1940年:布朗:维拉诺瓦

1940年:布朗:维拉诺瓦

我得到如下输出:

姓名:布朗赢:2输:0

年份:[1940]

姓名:布朗赢:0输:1

年份:[1939]

名称:维拉诺瓦赢:1输:1

年份:[1939年、1940年]

名称:维拉诺瓦赢:0输:1

年份:[1940]

当输出应类似于:

姓名:布朗赢:2输:1

名称:维拉诺瓦赢:1输:2

每支球队都有各自的胜负记录,而只有一个记录同时有胜负。我盯着我的代码看了好几个小时,似乎不明白它为什么这么做。谁能告诉我我犯了什么错误以及为什么会发生这种情况

谢谢大家!

这是我的主要课程:

import java.util.TreeMap;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Iterator;
public class Records {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws FileNotFoundException {
        File input = new File("C:\\Users\\Christian\\Desktop\\C code\\input4.txt");
        Scanner test = new Scanner(input).useDelimiter("[:\n]");
        TeamInfo tester;
        String year ="";
        String winner="";
        String loser="";
        TreeMap records = new TreeMap();
        TeamInfo temp = new TeamInfo(0,0,"");

        while(test.hasNext())
        {
            year = test.next();
            winner = test.next();
            loser = test.next();

                    /*search(test.next)
                    increment win
                    */
            if(!records.containsKey(winner))
            {

                TeamInfo firstRecord = new TeamInfo(0,0,"");
                firstRecord.incrementWins();
                firstRecord.addYear(year);
                records.put(winner, firstRecord);
                //  System.out.println("First iteration of " + winner);
            }
            else if(records.containsKey(winner))
            {
                temp = (TeamInfo)records.get(winner);
                temp.incrementWins();
                temp.addYear(year);
                records.replace(winner, temp);
                //  System.out.println("FOUND " + winner);
            }
                    /*
                    search(test.next)*/
            if(!records.containsKey(loser))
            {
                TeamInfo firstRecord = new TeamInfo(0,0,"");
                firstRecord.incrementLosses();
                firstRecord.addYear(year);
                records.put(loser, firstRecord);
                // System.out.println("First iteration of " + loser);
            }

            else if(records.containsKey(loser))
            {
                temp = (TeamInfo)records.get(loser);
                temp.incrementLosses();
                temp.addYear(year);
                records.replace(loser, temp);
                //    System.out.println("FOUND " + loser);
            }
                    /*increment loss
                    */
        }
        while(!records.isEmpty())
        {
            temp = (TeamInfo)records.get(records.firstKey());
            System.out.println("Name: " + records.firstKey().toString() + temp.toString());
            records.remove(records.firstKey());

        }
    }
}

一些有助于清理代码的一般性注释:1。处理地图时,您有重复的代码。您应该更喜欢这样:“item=map.get(key);如果item为null,item=newitem();map.put(key,item);use item”。2.你的物品有年份,但实际上没有使用它。3.使用集合时,请说明它们的类型,例如TreeSet或TreeMap,并首选它们的接口,如Set和Map。清理代码后,是时候进行调试了。您可以使用调试器或打印输出,但在任何情况下,不要盯着代码看,只需在代码运行时对其进行测试。感谢您提供的提示!我运行了调试器,发现扫描器正在(并依次)搜索树映射中的返回键和实际字符串。我将定界符改为/r而不是/n。