java - 如何将字符串中的双打添加到双打数组列表中?
问题描述
我必须为 students.txt 文件中的每一行添加一个学生,并将其中的成绩添加到新学生。学生对象有4个属性,name(字符串)、last name(字符串)、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);
推荐阅读
- php - PHP time() 显示不正确的时间,一小时后
- python - python vs nodejs的https实现有什么不同?
- android - ADB devices 命令显示我的 Galaxy S10 5G 的空列表
- r - 使用 data.table 和自定义函数从字符串变量中提取变量
- python - 在 Docker 中运行带有挂载文件系统的本地 Python 程序
- android - 错误:尝试将静态成员放入卡片标题第 18 行时,只能在初始化程序中访问它
- python - 更改 python 模块并重新安装它 - 不应用更改
- python - 如何在python中的api分页循环后编写可读的csv文件
- html - 使用 CSS3 动画链接缩进
- mysql - MySQL:在最近的“最近”日期加入