メソッド「readFile」でわかるように、問題を見つけることができません。スキャナが文字列を読み取ることが問題かどうかを確認しようとしましたが、プログラムをコンパイルすると、テキストファイルの名前、score.txt が表示されます。 、コンテンツではありません。これに約 3 時間取り組んできました。
package pointSystem;
import java.io.*;
import java.lang.*;
import java.util.*;
import main.Gooftw;
public class HighscoreManager {
private int currentHighScores[] = new int[5];
private int newScore = Gooftw.getScore();
private int temp;
//private Formatter formatter;
private Scanner sc;
private String file = "scores.txt";
File outfile;
FileOutputStream fop = null;
public HighscoreManager () {
System.out.println("Hello World!");
try {
//formatter = new Formatter(file);
sc = new Scanner(file);
} catch(Exception e){
System.out.println("you have an error");
}
//Read current highscores to game
}
public void readFile(){
int i = 0;
String values [] = new String [5];
while(sc.hasNext()) {
values[i] = sc.next();
System.out.println(values[i]);
/*System.out.println(sc.nextInt());
currentHighScores[i] = sc.nextInt();
System.out.println(currentHighScores[i]);*/
i++;
}
this.closeFile();
/*for(i=0;i<5;i++) {
System.out.println(currentHighScores[i]);
}
/*while(sc.hasNext()){
String a = sc.next();
String b = sc.next();
String c = sc.next();
System.out.printf(a,b,c);
}*/
}
public void addRecords(){
try{
outfile = new File(file);
fop = new FileOutputStream(outfile);
if(!outfile.exists()){
outfile.createNewFile();
}
for(int i = 0; i<5; i++){
//formatter.format("%s \n", currentHighScores[i] );
fop.write(currentHighScores[i] );
}
//fop.flush();
fop.close();
}catch(IOException e){
e.printStackTrace();
} finally {
try{
if(fop!=null){
fop.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
}
//formatter.close();
public void compareScores(){
if(newScore > currentHighScores[0]){
temp = currentHighScores[0];
currentHighScores[0] = newScore;
}
for(int y = 1 ; y<5 ; y++){
if(temp < currentHighScores[y]){
temp = temp + currentHighScores[y];
currentHighScores[y] = temp - currentHighScores[y];
temp = temp - currentHighScores[y];
}
}
}
public void closeFile(){
sc.close();
}
}