java - 如何拆分多行并将它们添加到arraylist
问题描述
我在从 txt 文件中分离信息时遇到任何问题,将该信息添加到列表并使用该信息创建对象
此应用程序的数据文件将是一个逗号分隔(每条数据将由一个逗号分隔)的纯文本文件。为了标记成绩类型(实验室、项目和测试成绩)的差异,将包含一个空数据字段。文件的第一行将包含练习的可能分数。学生 ID 将是字符串。
示例 ID,名字,姓氏,10,10,10,,100,100,,100
三个实验室(每个值 10 分)
两个项目(每个100分)
一项测试(价值 100 分)
txt文件设置如下
ID,名字,姓氏,10,10,10,,100,100,,100
1234,乔,学生,10,8,9,,100,90,,100
5678,另一个,学生,7,7,7,,75,75,,75
9012,Yet,Another,10,10,10,,65,70,,50
对象类
public class Student {
private String identifcation;
private String firstName;
private String lastName;
private List labs;
private List project;
private List test;
public Student(String identifcation, String firstName, String lastName, List<Integer> labs, List<Integer> projects, List<Integer> test) {
this.identifcation = identifcation;
this.firstName = firstName;
this.lastName = lastName;
this.labs = labs;
this.project = projects;
this.test = test;
}
}
类中的 readfile 方法
public List<Student> decode() {
List<Student> students = new ArrayList<Student>();
while (this.scan.hasNext()) {
List<Integer> labs = new ArrayList<Integer>();
List<Integer> projects = new ArrayList<Integer>();
List<Integer> tests = new ArrayList<Integer>();
String identifcation = this.scan.next();
String firstName = this.scan.next();
String lastName = this.scan.next();
//help int lab = the labs
//help labs.add(lab); should add the 3 labs
//help int project =
//help projects.add(project); should add the 2 projects
//help int test =
//help tests.add(test); should add the 1 test
System.out.println(identifcation);
System.out.println(firstName);
System.out.println(lastName);
System.out.print(this.scan.nextLine());
Student real = new Student(identifcation, firstName, lastName, labs, projects, tests);
students.add(real);
}
return students;
}
打印语句给了我一个输出
身份证,第一
姓名,姓氏
名称,10,10,10,,100,100,,100
1234,乔,学生,7,8,9,,80,90,,100
5678,另一个,学生,7,7,7,,75,75,,75
9012,Yet,Another,10,10,10,,65,70,,50
他们应该给
ID
名
姓
ETC..
解决方案
I'd recommend going line by line in with the scanner - store the entire line as an input string. Then you could split up the line using a function like the Java string.split(), putting in ',' for the separator option. This would allow you to return the string in an array, which you can then sort into your arraylist however you wanted. See here for more detail on the split() function
example (might want to check in a compiler):
Scanner in = new Scanner(System.in);
String input;
//define arraylist outside of loop :-)
ArrayList<String> arraylist1 = new ArrayList<>();
ArrayList<String> arraylist2 = new ArrayList<>();
(while in.hasNext()){
//store entire line
input= in.nextLine();
//Splits input string with , stores each string in an index position in data[]
String[] data = input.split(",");
arraylist1.add(data[0]);
arraylist2.add(data[1]);
//etc....
}
Hope this helps!
推荐阅读
- java - Spring Data Elasticsearch 4.2.x:DateFormat 抛出空指针
- javascript - 如何使用 d3-contour 绘制简单的 2D 等高线图?
- wcag - 为了确保 WCAG 1.4.11“非文本对比度”的通过对比度,我是否将 :hover 的“颜色”属性与非悬停颜色或背景进行比较?
- r - 在拆分元素之前合并
- javascript - [[scope]] 键内的块是什么以及它是何时创建的?
- scikit-learn - 更改 OneclassSVM 中预测函数的输出类标签值
- dynamic - 在数据库中存储页脚部分
- flutter - 在 BLoC 中打开数据库表,在 MyApp 中处理 - 这有效吗?
- video-streaming - 在 Dash.JS 中处理视频的代码部分是什么?
- angular - 升级到 Angular 12 后的 Web Worker 问题