[英]How to convert parsed string into enum type as an argument in java
I am new to java and practicing parsing csv file.我是 java 和练习解析 csv 文件的新手。 Now, I've successfully parsed the file into columns, but I need to convert the string into the enum type as an argument.
现在,我已经成功地将文件解析为列,但我需要将字符串转换为枚举类型作为参数。
The file is like this:文件是这样的:
Profession //the header
boxer
nurse
doctor
.
.
.
I have an enum type Profession
:我有一个枚举类型
Profession
:
enum {BOXER,NURSE,DOCTOR,UNKNOWN,...,...,...}
And I've parsed the data like this:我已经解析了这样的数据:
ArrayList<String> csvContents = new ArrayList<String>();
try (BufferedReader csvReader = new BufferedReader(new FileReader(csvFile));) {
String headerLine = csvReader.readLine(); //get rid of the header
while ((line = csvReader.readLine()) != null) {
csvContents.add(line);
}
for (String csvLine : csvContents) {
String[] data = csvLine.split("\\s*,\\s*"); // split by comma and remove redundant spaces
String profession = data[0].toUpperCase(); // String type
Person(Profession data[0], otherArgument); // need to be Profession enum type
}
I've already known that I can get the value by doing this:我已经知道我可以通过这样做来获得价值:
Profession.valueOf(data[0].toUpperCase);
But how can I convert this value into Profession
enum type?但是如何将此值转换为
Profession
枚举类型? Any help or hint is highly appreciated.非常感谢任何帮助或提示。
Profession professionEnum = Profession.valueOf(data[0].toUpperCase);
this will give you enum type which you pass to your method as below:这将为您提供枚举类型,您将其传递给您的方法,如下所示:
Person(professionEnum, otherArgument);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.