[英]Writing a file in Java
Good day! 美好的一天!
I have a project (game) that needs to be presented tomorrow morning. 我有一个项目(游戏),明天早上需要提交。 But I discovered a bug when writing in the high scores. 但是我在高分写作时发现了一个错误。 I am trying to create a text file and write the SCORE NAME in descending order using score as the basis. 我正在尝试创建一个文本文件,并以分数为基础按降序写入SCORE NAME。
FOR EXAMPLE: 例如:
SCORE NAME RANK
230 God
111 Galaxian
10 Gorilla
5 Monkey
5 Monkey
5 Monkey
NOTE THERE'S ALSO A RANK My code is as follows: 注意还有一个我的代码如下:
public void addHighScore() throws IOException{
boolean inserted=false;
File fScores=new File("highscores.txt");
fScores.createNewFile();
BufferedReader brScores=new BufferedReader(new FileReader(fScores));
ArrayList vScores=new ArrayList();
String sScores=brScores.readLine();
while (sScores!=null){
if (Integer.parseInt(sScores.substring(0, 2).trim()) < score && inserted==false){
vScores.add(score+"\t"+player+"\t"+rank);
inserted=true;
}
vScores.add(sScores);
sScores=brScores.readLine();
}
if (inserted==false){
vScores.add(score+"\t"+player+"\t"+rank);
inserted=true;
}
brScores.close();
BufferedWriter bwScores=new BufferedWriter(new FileWriter(fScores));
for (int i=0; i<vScores.size(); i++){
bwScores.write((String)vScores.get(i), 0, ((String)vScores.get(i)).length());
bwScores.newLine();
}
bwScores.flush();
bwScores.close();
}
But if i input three numbers: 60 Manny, the file would be like this: 但是,如果我输入三个数字:60 Manny,则文件将如下所示:
60 Manny
230 God
111 Galaxian
10 Gorilla
5 Monkey
5 Monkey
5 Monkey
The problem is it can only read 2 numbers because I use sScores.substring(0, 2).trim())
. 问题是它只能读取2个数字,因为我使用sScores.substring(0, 2).trim())
。 I tried changing it to sScores.substring(0, 3).trim())
. 我尝试将其更改为sScores.substring(0, 3).trim())
。 but becomes an error because it read upto the character part. 但由于读取到字符部分而成为错误。 Can anyone help me in revising my code so that I can read upto 4 numbers? 谁能帮助我修改代码,以便我最多读取4个数字? Your help will be highly appreciated. 非常感谢您的帮助。
What you should use is: 您应该使用的是:
String[] parts = sScrores.trim().split("\\s+", 2);
Then you will have an array with the number at index 0, and the name in index 1. 然后,您将得到一个数组,其数字在索引0处,名称在索引1处。
int theNumber = Integer.parseInt(parts[0].trim();
String theName = parts[1].trim();
You could re-write the while-loop like so: 您可以像这样重新编写while循环:
String sScores=brScores.readLine().trim();
while (sScores!=null){
String[] parts = sScrores.trim().split(" +");
int theNumber = Integer.parseInt(parts[0].trim();
if (theNumber < score && inserted==false){
vScores.add(score+"\t"+player+"\t"+rank);
inserted=true;
}
vScores.add(sScores);
sScores=brScores.readLine();
}
Personally, I would add a new HighScore
class to aid in parsing the file. 就个人而言,我将添加一个新的HighScore
类以帮助解析文件。
class HighScore {
public final int score;
public final String name;
private HighScore(int scoreP, int nameP) {
score = scoreP;
name = nameP;
}
public String toString() {
return score + " " + name;
}
public static HighScore fromLine(String line) {
String[] parts = line.split(" +");
return new HighScore(Integer.parseInt(parts[0].trim()), parts[1].trim());
}
}
The format of each line is always the same : an integer, followed by a tab, followed by the player name. 每行的格式始终相同:一个整数,后跟一个制表符,然后是播放器名称。
Just find the index of the the tab character in each line, and substring from 0 (inclusive) to this index (exclusive), before parsing the score. 只需在解析分数之前找到每行中制表符的索引,然后从0(含)到此索引(不包含)的子字符串。
The player name can be obtained by taking the substring from the tab index +1 (inclusive) up the the length of the line (exclusive). 玩家名称可以通过从选项卡索引+1(含)开始沿行的长度(不含)的子字符串获得。
if above mentioned table is a file. 如果上面提到的表是一个文件。
for first two score it will be fine but for 5 it start reading character. 对于前两个分数,这会很好,但是对于5,它将开始读取字符。 Might be that is causing problem. 可能是造成问题的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.