情報 :
単純なファイルリーダーであるプログラムを実行すると、例外が発生します。
.javaファイルの横にファイルを保存しています。
出力:
run:
name:Koen
スコア:44
スレッド "main"の例外java.util.NoSuchElementException:行が見つかりません
name:Kevin
スコア:55
at java.util.Scanner.nextLine(Scanner.java:1585)
at tetris.FileIO.loadHighscores (FileIO.java:41)at
tetris.FileIO.getLineScores(FileIO.java:28)
at tetris.FileIO.main(FileIO.java:62)
Java結果:1
BUILD SUCCESSFUL(合計時間:0秒)
コード:
package tetris;
import java.io.File;
import java.io.InputStream;
import java.io.Writer;
import java.util.Scanner;
public class FileIO {
private File file;
private Scanner filescScanner, lineScanner;
private Writer fileWriter, lineWriter;
private String[][] data;
public FileIO () {
String[][] data = new String[100][1];
}
public String[][] getLineScores(){
return this.loadHighscores(this.getClass().getResourceAsStream("LineHighscores.txt"));
}
public String[][] getTimeScores(){
return this.loadHighscores(this.getClass().getResourceAsStream("TimeHighscores.txt"));
}
public String[][] loadHighscores( InputStream resourceStream){
int x=0;
String test = "";
filescScanner = new Scanner(resourceStream);
while((test=filescScanner.nextLine())!=null) {
lineScanner = new Scanner(test);
lineScanner.useDelimiter("-/-");
System.out.println("name : "+lineScanner.next());
System.out.println("score : " +lineScanner.next());
x++;
}
lineScanner.close();
filescScanner.close();
return data;
}
public static void main(String[] args){
FileIO file = new FileIO();
System.out.println(file.getLineScores());
}
}