Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何对Java文件中的数据进行排序?_Java_Arrays_Javafx - Fatal编程技术网

如何对Java文件中的数据进行排序?

如何对Java文件中的数据进行排序?,java,arrays,javafx,Java,Arrays,Javafx,我的代码是一个游戏,它在一个.txt文件中注册了玩家的名字和分数,所以我尝试创建一个新类来对分数进行排序,在游戏开始时创建一个“最高分数”按钮,并在旁边显示最高分数和相应的名字。 寄存器方法基本上是这样的: Label text = new Label("How much players will play game with game?"); TextField input = new TextField(); Button submit = new Butto

我的代码是一个游戏,它在一个.txt文件中注册了玩家的名字和分数,所以我尝试创建一个新类来对分数进行排序,在游戏开始时创建一个“最高分数”按钮,并在旁边显示最高分数和相应的名字。 寄存器方法基本上是这样的:

Label text = new Label("How much players will play game with game?");
        TextField input = new TextField();
        Button submit = new Button("Submit");
        submit.setOnAction(event -> {
            tPlayer = Integer.parseInt(input.getText());
            window.close();
        });
因此,该文件将如下所示:

,Player1,56
,player1,18
,player2,61
,noobMaster69,82
然后,还有一个类使用read方法:

try {
            File myObj = new File("Jugador.txt");
            Scanner myReader = new Scanner(myObj);
            while (myReader.hasNextLine()) {
                // Im stuck here
            }
            myReader.close();
        } catch (FileNotFoundException e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
}
所以,仅此而已,我不知道如何继续,任何帮助都将不胜感激


(很抱歉,如果有任何格式错误或类似的错误,我在这里有点不知所措:D)。

我要做的第一件事是创建一个类来表示
分数。比如:

public class Score {

    String player;
    float score;

    public Score(String player, float score) {
        super();
        this.player = player;
        this.score = score;
    }

}
请注意,这只是一个例子。一个好的
Score
类实现不应该将
Player
表示为字符串。做出相应的改变

之后,我们需要创建
Score
类来实现
compariable
接口。这表明
得分
实例可以与其他对象进行比较;特别是在我们的例子中,类型为
Score
的其他对象

public class Score implements Comparable<Score>{

    String player;
    float score;

    public Score(String player, float score) {
        super();
        this.player = player;
        this.score = score;
    }

    @Override
    public int compareTo(Score anotherScore) {
        if(this.score > anotherScore.score) {
            return 1;
        } else if (this.score < anotherScore.score) {
            return -1;
        }
        return 0;
    }

}
您应该创建一个
Score
实例并将其添加到集合中

List<Score> scoreList = new ArrayList<>();
while (myReader.hasNextLine()) {
    String line = myReader.nextLine();
    String[] splitLine = line.split(",");
    // line = Player1,56
    // splitLine = ["Player1", "56"]
    Score score = new Score(splitLine[0], Float.parseFloat(splitLine[1]));
    scoreList.add(score);
}
Collections.sort(scoreList); // Sorts in ASCENDING order
Collections.sort(scoreList, Collections.reverseOrder()) // Sorts in DESCENDING order
List scoreList=new ArrayList();
while(myReader.hasNextLine()){
String line=myReader.nextLine();
String[]splitLine=line.split(“,”);
//line=播放器1,56
//分割线=[“Player1”,“56”]
分数=新分数(分割线[0],Float.parseFloat(分割线[1]);
分数表。添加(分数);
}
集合。排序(记分表);//按升序排序
Collections.sort(scoreList,Collections.reverseOrder())//按降序排序

Collections.sort()
将使用您在
Score
中创建的
compareTo()
方法对列表进行升序排序。

创建一个代表球员的类,包含姓名和分数。然后研究如何从字符串中解析信息。然后研究如何根据不同的属性对Java对象进行排序。这些是你必须学习的不同任务。请理解这里很少有人愿意为你工作。看看每个步骤,并逐一解决它们。理想情况下,为每个问题编写小型的单独测试类。每件事都要自己解决,而且只有到最后,当一切都正常时,才能将你的部分集成到一个程序中。这是我所要求的,而不是我工作中的某个人。感谢阅读列表。提供用于排序的比较器来对列表进行排序。保存已排序的列表。
List<Score> scoreList = new ArrayList<>();
while (myReader.hasNextLine()) {
    String line = myReader.nextLine();
    String[] splitLine = line.split(",");
    // line = Player1,56
    // splitLine = ["Player1", "56"]
    Score score = new Score(splitLine[0], Float.parseFloat(splitLine[1]));
    scoreList.add(score);
}
Collections.sort(scoreList); // Sorts in ASCENDING order
Collections.sort(scoreList, Collections.reverseOrder()) // Sorts in DESCENDING order