如何将双精度从字符串添加到双精度数组列表?



我必须为students.txt文件中的每一行添加一个学生,并将其中的成绩添加到新学生。student对象有4个属性,name(string), last name(string), number(int)和grades (arraylist double)

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class readFile {
public static void main(String[] args) {
ArrayList<student> students = new ArrayList<>();
int linecnt = 0;
try(BufferedReader br = new BufferedReader(new FileReader("students.txt"))) {
String line;
while((line = br.readLine()) != null) {

students.add(new student("", "", 0));
Scanner scan = new Scanner(line);
if(scan.hasNextDouble()) {
students.get(linecnt).addGrade(scan.nextDouble());
}
linecnt++;

}

}   catch(FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


}
}

当我运行这个什么也没有发生,没有错误,只是一个空白的空白。有什么问题吗?

您还没有说明应该输出什么。你的问题的答案

如何将双精度从字符串添加到双精度数组列表?

String text = "12.34"; 
double value = Double.parseDouble(text);
arraylist.add(value);

最新更新