Hallo.
Ich programmiere gerade ein Spiel bei dem ich gerne eine Highscore-Liste machen würde. Ich wollte die Ergebnisse in ein txt-file speichern, jedoch muss dieses beim Spielstart immer eingelesen werden. Jetzt versuche ich es über Preferences aber irgendwie weiß ich nicht so wirklich weiter. Hier mein Code:
Bitte um Hilfe.
Danke im Voraus.
Liebe Grüße, JavaKüken
Ich programmiere gerade ein Spiel bei dem ich gerne eine Highscore-Liste machen würde. Ich wollte die Ergebnisse in ein txt-file speichern, jedoch muss dieses beim Spielstart immer eingelesen werden. Jetzt versuche ich es über Preferences aber irgendwie weiß ich nicht so wirklich weiter. Hier mein Code:
Code:
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;
public class Highscore {
private int[] highscore = new int[10];
private int place = 0; //place in the highscore list
private boolean found = false; //variable if the place is found
private OutputStreamWriter out;
//constructor
public Highscore() {
for (int i = 0; i < 10; i++) {
highscore[i] = 0; // set all places to null
}
}
public void writeIn(int newScore) {
while(found == false || place == 9) { //search for place to write enter the highscore
if (highscore[place] <= newScore) { //search for place where the highscore is smaller than the newscore
found = true;
//exception for the last place
if(place==9) {
highscore[9] = newScore;
}
} else {
for (int i = 8; i==place; i--) {
highscore[i+1] = highscore[i];
}
highscore[place] = newScore;
//add new score to the list on the right place
}
place++;
}
}
//return the 1. place of the highscore array
public int getFirstPlace() {
return highscore[1];
}
public void write() {
File outputFile = new File("highscore.txt");
try {
FileWriter out = new FileWriter(outputFile);
} catch (IOException e) {
e.printStackTrace();
}
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void writeHighscore() {
Preferences prefs = Preferences.systemRoot();
highscore = prefs.getInt("Highscore",)
}
try {
prefs.flush();
} catch (BackingStoreException e) {
e.printStackTrace();
}
}}
Bitte um Hilfe.
Danke im Voraus.
Liebe Grüße, JavaKüken