[英]Read CSV containing Strings & int to arraylist with try/catch
我希望有些人能够为我正在开发的程序建议一种替代方法。 我已经完成了以下测试代码来显示我的查询,而无需发布我的完整代码。
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class Main {
ArrayList<Students> chemistry = new ArrayList<>();
public void loadClass() {
try (BufferedReader br = new BufferedReader(new FileReader("Chemistry.csv"))) {
String line;
line = br.readLine();
while ((line = br.readLine()) != null) {
String[] values = line.split(",");
try {
chemistry.add(new Students(values[0], values[1], Integer.parseInt(values[2]),
Integer.parseInt(values[3]), Integer.parseInt(values[4])));
} catch (NumberFormatException e) {
System.out.println("You have some text in a number field for entry: " + values[0] + " " + values[1]);
}
}
} catch (FileNotFoundException e) {
System.out.println("The file has not been found in the path \"" + (new File(".").getAbsoluteFile()) + "\"");
System.out.println("Please place the CSV file at this location");
} catch (IOException e) {
System.out.println("Sorry an input/output error has occured.");
e.printStackTrace();
}
System.out.println("students loaded from the file are:");
for (Students pupils : chemistry) {
System.out.println("Student " + pupils.getFirstName() + " " + pupils.getSurname() + " was born in "
+ pupils.getBirthYear() + "/" + pupils.getBirthMonth() + " /"
+ pupils.getBirthDay());
}
System.out.println("\nThe number of students in the class is: " + chemistry.size());
}
public static void main(String[] args) {
Main main = new Main();
main.loadClass();
}
}
public class Students {
private String firstName;
private String surname;
private int birthYear;
private int birthMonth;
private int birthDay;
// All-args constructor, getters and setters omitted for brevity
}
这是我在Chemistry.csv文件中使用的 CSV 数据
First Name,Surname,Year of Birth,Month of Birth,Day of Birth
Joe,Bloggs,2002,12,8
Jane,Bloggs,2003,11,15
Harry,Smith,2004,10,2
William,Wallace,2001,9,7
Philip,Jones,2002,Aug,9th
正如您在我的 CSV 文件中看到的那样,我特意在最后一行输入了月份和日期的字符串条目。 这是为了测试我的 NumberFormatException 尝试/捕获。
它可以工作,并且提示用户该行条目存在错误,但这是一个小数据集。 我想知道当可能有数百行时我能做什么。
我在想,在 catch 中,我可以有一些东西可以用有效的 int 数替换无效的字符串。 我知道在这种情况下,我必须设计一个 if/else 或 switch 语句来说明 12 个月和 30(ish)天,但是我现在只想输入“0”。
我不确定如何将int 0作为默认值传递,因为我的chemistry.add行解析文件。
我还认为可能有另一种方法可以将值输入到我的 ArrayList 中,因此我可以将 try/catch 放在Integer.parseInt位周围,但同样,我不确定另一种方法。
任何人都可以提出一个很好的调查选择吗?
编辑:在下面 g00se 发表评论后,我改用它,它似乎有效:
while ((line = br.readLine()) != null) {
int third, fourth, fifth;
String[] values = line.split(",");
String first = values[0];
String second = values[1];
try {
third = Integer.parseInt(values[2]);
} catch (NumberFormatException e3) {
third = 0;
}
try {
fourth = Integer.parseInt(values[3]);
} catch (NumberFormatException e4) {
fourth = 0;
}
try {
fifth = Integer.parseInt(values[4]);
} catch (NumberFormatException e5) {
fifth = 0;
}
chemistry.add(new Student(first, second, third, fourth, fifth));
}
需要一些整理,但至少我有一个前进的道路......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.