Java 如何控制比较器以避免内部的&引用;在csv文件中
我试图按年龄对这些值进行排序,结果出现了一个错误。程序中有一种方法,它用“,”分隔值。在检查错误时,它似乎还考虑了字符串地址内的(不应该这样做)。关于如何使它忽略双引号内的值,有什么想法吗 下面是我拆分值的代码部分:Java 如何控制比较器以避免内部的&引用;在csv文件中,java,sorting,comparator,Java,Sorting,Comparator,我试图按年龄对这些值进行排序,结果出现了一个错误。程序中有一种方法,它用“,”分隔值。在检查错误时,它似乎还考虑了字符串地址内的(不应该这样做)。关于如何使它忽略双引号内的值,有什么想法吗 下面是我拆分值的代码部分: while ((string = input.readLine()) != null) { // Reads each line and loops until there is no more text to be read String[] list = st
while ((string = input.readLine()) != null) { // Reads each line and loops until there is no more text to be read
String[] list = string.split(",", 8); // Uses "," to split the data
String name = list[0] + "," + list[1]; // The parts of data is assigned their specific variable
String email = list[2];// The parts of data is assigned their specific variable
String address = list[3];
int age = Integer.parseInt(list[4]); // Using parseInt, the data becomes an integer so comparison will be possible
String residency = list[5];
int district = Integer.parseInt(list[6]);
String gender = list[7];
person.add(new Person(name, email, address, age, residency, district, gender)); // Passes the information from the txt file
}
改用CSV解析器。解析带有引号和嵌入逗号的CSV并不容易。使用库(如)解析数据。如果您的目的是学习或练习,则可以使用标志
boolean isinquetes=false
。然后,您可以手动迭代字符,并在遇到“
时切换标志。请注意,这不适用于嵌套引号。