Java filing and highscore

会有一股神秘感。 提交于 2020-01-03 04:38:12

问题


Hi so I have an assignment due in 6 hours and I am completely lost. I have new methods added to my existing work done so far but I don't know how to do them. The text file has names and scores, and the first method needs to check if the score is among the top 10 and return true/false:

the last method that I am lost in needs to write the high score into highscore.txt file, at the correct position(in descending order), the method uses readHighScore() method to find the line number where the record should be inserted.

public static void recordHighScore(String name, int score){
    }

回答1:


You can't insert data into text files. You can only append/rewrite. These are the steps:

  • Read data and store into objects.
  • Get the highest/lowest from the objects (you can also sort them with Collections.sort() or Arrays.sort())
  • Write the sorted objects back into the text file. (You will be overwriting the text file with new data)


来源:https://stackoverflow.com/questions/34241413/java-filing-and-highscore

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!