こんにちは、 '、'で区切られた名前を持つファイル(以下の形式を参照)からデータを読み取るメソッドを作成しています。 java.util.NoSuchElementExceptionJava fileReader - 文字列の後にintを読み取る
は、誰かが私が間違っているつもりだところを教えてもらえ - メソッドは名前が細かい出力が、私はそれの後に整数を追加したら、それはエラーが発生していると呼ばれる
。ありがとう。
更新日 ありがとうございました。問題が解決しました。
こんにちは、 '、'で区切られた名前を持つファイル(以下の形式を参照)からデータを読み取るメソッドを作成しています。 java.util.NoSuchElementExceptionJava fileReader - 文字列の後にintを読み取る
は、誰かが私が間違っているつもりだところを教えてもらえ - メソッドは名前が細かい出力が、私はそれの後に整数を追加したら、それはエラーが発生していると呼ばれる
。ありがとう。
更新日 ありがとうございました。問題が解決しました。
あなたがString name = in.nextLine();
を呼び出すときので、代わりにこれを試して、それは完全なライン「名、5,6,1970」をロードしますので、もう一度int DD = in.nextInt();
を呼び出すと、何も見つけないだろう、と例外
がスローされます。
public void readFile() {
while(in.hasNext()) {
String line = in.nextLine();
String[] values = line.split(",");
String name = values[0];
int DD = Integer.parseInt(values[1]);
int MM = Integer.parseInt(values[2]);
int YYYY = Integer.parseInt(values[3]);
System.out.println(name + DD + MM + YYYY);
}
}
は、以下のことを試してみてください。
String line = "name,5,6,1970"; //the whole line
String[] parts = line.split(",");
String name = parts[0]; //name
int DD = Integer.parseInt(parts[1]); //5
int MM = Integer.parseInt(parts[2]); //6
int YYYY = Integer.parseInt(parts[3]); //1970
別の方法としては、スキャナ
を使用することですwhile(in.hasNext()) {
Scanner sc = new Scanner(in.nextLine());
sc.useDelimiter(",");
String name = sc.next();
int DD = sc.nextInt();
int MM = sc.nextInt();
int YYYY = sc.nextInt();
System.out.println(name + DD + MM + YYYY);
}