gpt4 book ai didi

java - ParseEnum(方法)无法从 SuperCSV (Java) 中的 .csv 文件解析/读取字符串作为枚举

转载 作者:行者123 更新时间:2023-11-30 07:12:41 25 4
gpt4 key购买 nike

这个周末我尝试了你很棒的框架,并想在生产中使用它。不幸的是,我的Employee Bean有一个属性sexity,其类型为enum。不知何故,集成的官方 ParseEnum cellprocessor 在读取 .csv 文件时无法解析性属性。可以请您研究一下吗?

错误消息

Exception in thread "main" org.supercsv.exception.SuperCsvCellProcessorException: ' BI' could not be parsed as a enum of type test.supercsv.Sexuality
processor=org.supercsv.cellprocessor.ParseEnum
context={lineNo=2, rowNo=2, columnNo=5, rowSource=[1, Pankaj Kumar, CEO, 5,000USD, BI]}
at org.supercsv.cellprocessor.ParseEnum.execute(ParseEnum.java:146)
at org.supercsv.util.Util.executeCellProcessors(Util.java:93)
at org.supercsv.io.AbstractCsvReader.executeProcessors(AbstractCsvReader.java:203)
at org.supercsv.io.CsvBeanReader.readIntoBean(CsvBeanReader.java:261)
at org.supercsv.io.CsvBeanReader.read(CsvBeanReader.java:190)
at test.supercsv.Reading.readWithCsvBeanReader(Reading.java:35)
at test.supercsv.Reading.main(Reading.java:24)
<小时/>

部分代码

枚举 CellProcessor

 private static CellProcessor[] getProcessors() {

final CellProcessor[] processors = new CellProcessor[]{
new UniqueHashCode(), // ID (must be unique)
new NotNull(), // Name
new Optional(), // Role
new NotNull(), // Salary
new ParseEnum(Sexuality.class, true) // enum Sexuality
};

return processors;
}

csv 文件中的第一个 Employee 对象

1,Pankaj Kumar,CEO,"5,000USD", BI
<小时/>

完整代码

员工 Bean

package test.supercsv;

public class Employee {

private String id;
private String name;
private String role;
private String salary;
private Sexuality sexuality;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getRole() {
return role;
}

public void setRole(String role) {
this.role = role;
}

public String getSalary() {
return salary;
}

public void setSalary(String salary) {
this.salary = salary;
}

public Sexuality getSexuality() {
return sexuality;
}

public void setSexuality(Sexuality sexuality) {
this.sexuality = sexuality;
}

@Override
public String toString() {
return "Employee{" + "id=" + id + ", name=" + name + ", role=" + role + ", salary=" + salary
+ ", sexuality=" + sexuality.name()
+ '}';
}

}

枚举性行为

package test.supercsv;

public enum Sexuality {

HETERO, HOMO, BI, TRANSGENDER;

}

CSVBeanReader

package test.supercsv;

import java.io.FileReader;
import java.io.IOException;
import org.supercsv.cellprocessor.Optional;
import org.supercsv.cellprocessor.ParseEnum;
import org.supercsv.cellprocessor.constraint.NotNull;
import org.supercsv.cellprocessor.constraint.UniqueHashCode;
import org.supercsv.cellprocessor.ift.CellProcessor;
import org.supercsv.io.CsvBeanReader;
import org.supercsv.io.ICsvBeanReader;
import org.supercsv.prefs.CsvPreference;

public class Reading {

private static final String CSV_FILENAME = "src/resources/employee.csv";

public static void main(String[] args) throws IOException {
readWithCsvBeanReader();
}

private static void readWithCsvBeanReader() throws IOException {
try (ICsvBeanReader beanReader = new CsvBeanReader(new FileReader(CSV_FILENAME), CsvPreference.STANDARD_PREFERENCE)) {

// the header elements are used to map the values to the bean (names must match)
final String[] header = beanReader.getHeader(true);
final CellProcessor[] processors = getProcessors();

Employee employee;
while ((employee = beanReader.read(Employee.class, header, processors)) != null) {
System.out.println(String.format("lineNo=%s, rowNo=%s, employee=%s",
beanReader.getLineNumber(),
beanReader.getRowNumber(),
employee));
}

}
}

private static CellProcessor[] getProcessors() {

final CellProcessor[] processors = new CellProcessor[]{
new UniqueHashCode(), // ID (must be unique)
new NotNull(), // Name
new Optional(), // Role
new NotNull(), // Salary
new ParseEnum(Sexuality.class, true) // enum Sexuality
};

return processors;
}
}

CSV 文件的内容

ID,Name,Role,Salary,Sexuality
1,Pankaj Kumar,CEO,"5,000USD", BI
2,Lisa,Manager,500USD, Homo
3,David,,1000USD, Hetero

最佳答案

解决方案

就像 Hound Dog已指出:我的 csv 文件中的空格是造成问题的原因。只需删除它们,代码就可以运行了!

ID,Name,Role,Salary,Sexuality
1,Pankaj Kumar,CEO,"5,000USD,BI
2,Lisa,Manager,500USD,Homo
3,David,,1000USD,Hetero

关于java - ParseEnum(方法)无法从 SuperCSV (Java) 中的 .csv 文件解析/读取字符串作为枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38947629/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com