有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JavaEclipse从一个文件中获取名称和数字,按字母顺序排序到一个新文件中,按数字排序到一个新文件中

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

我的文件,玩家和;分数txt包含以下内容:

Bruce 127

Elayna 144

Lisa 153

Marcus 188

Amber 133

Ryan 149

Dorian 099

Joel 175

Jenna 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);

    }

}

我可能把事情搞砸了,或者我请求帮助时看起来很傻。我只是不明白这一部分以及如何做到这一点。感谢您的帮助


共 (1) 个答案

  1. # 1 楼答案

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

    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);
            }
    
        }
    
    }
    

    您可以获得阵列的更多详细信息。sort()here