2017-05-11 10 views
0

こんにちは、 '、'で区切られた名前を持つファイル(以下の形式を参照)からデータを読み取るメソッドを作成しています。 java.util.NoSuchElementExceptionJava fileReader - 文字列の後にintを読み取る

は、誰かが私が間違っているつもりだところを教えてもらえ - メソッドは名前が細かい出力が、私はそれの後に整数を追加したら、それはエラーが発生していると呼ばれる

。ありがとう。

更新日 ありがとうございました。問題が解決しました。

答えて

0

あなたが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); 
     } 
    } 
0

は、以下のことを試してみてください。

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 
1

別の方法としては、スキャナ

を使用することです
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); 
} 
関連する問題