3

我这个学期刚开始在大学学习 Java,虽然我可以做一些更基本的事情,我可以编写简单的程序,但我在编写这个程序时遇到了障碍。我还没有了解树(似乎是一个常见的修复方法),我需要帮助排序东西和创建新文件。

我的文件 Players&Score.txt 包含以下内容:

布鲁斯 127

伊莱娜 144

丽莎 153

马库斯 188

琥珀色 133

瑞恩 149

多里安 099

乔尔 175

珍娜 101

所以基本上我需要根据名称顺序将这个列表排序在一个文件中,然后根据分数。到目前为止,我的代码看起来像这样。

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

/**
 * Bruce P., November 14, 2013, CSC 131
 * This program uses a file consisting of members of a bowling league. Each line has one name and the average score of the player.
 * The program reads the file and then creates two new files in which one is sorted by alphabetical order of the player's name
 * and the other is sorted in numerical order based on the player's score.
 *
 */

public class MembersAndScores 
{

    public static void main(String[] args) throws IOException
    {
        File inputFile = new File("Players&Score.txt");
        if (!inputFile.exists())
        {
            System.out.println("File Players&Score.txt was not found.");
            System.exit(0);
        }

        Scanner input = new Scanner(inputFile);
        String line;

        File scoreSort = new File("SortedByName.txt");
        PrintWriter writer = new PrintWriter(scoreSort);

    }

}

我可能搞砸了,或者我看起来真的很愚蠢寻求帮助。我只是不明白这部分以及如何做到这一点。任何帮助表示赞赏。

4

1 回答 1

0

这将根据名称进行排序。我将很快添加分数代码。

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

/**
 * Bruce P., November 14, 2013, CSC 131
 * This program uses a file consisting of members of a bowling league. Each line has one name and the average score of the player.
 * The program reads the file and then creates two new files in which one is sorted by alphabetical order of the player's name
 * and the other is sorted in numerical order based on the player's score.
 *
 */

public class MembersAndScores 
{

    public static void main(String[] args) throws IOException
    {

        try {
            File inputFile = new File("Players&Score.txt");
            if (!inputFile.exists())
            {
                System.out.println("File Players&Score.txt was not found.");
                System.exit(0);
            }

            BufferedReader input=new BufferedReader(new FileReader(inputFile));
            String line;
            List<String> stl=new LinkedList();
            while(true){
                line=input.readLine();
                if(line!=null&&line.length()>1)
                stl.add(line);
                if(line==null)
                    break;
            }

            data=new String[stl.size()];
            for (int i=0;i<stl.size();i++) {
                data[i]=stl.get(i);
            }

            Arrays.sort(data);
            for (String string : data) {
                System.out.println(string);
            }

            File scoreSort = new File("SortedByName.txt");
            PrintWriter writer = new PrintWriter(scoreSort);
            for (String string : data) {
                writer.write(string+"\n");
            }

            writer.close();

        } catch (Exception ex) {
            Logger.getLogger(MembersAndScores.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

}

您可以在此处获取 Arrays.sort() 的更多详细信息

于 2013-11-14T01:45:48.253 回答