gpt4 book ai didi

java - 将字符串转换为枚举以允许读取文件

转载 作者:行者123 更新时间:2023-12-02 01:20:12 26 4
gpt4 key购买 nike

我正在编写需要获取文件输入然后将值重建到 ArrayList 中的代码。

这是迄今为止将文件转换为对象的代码。

  public static ArrayList<User> fileToObject() {

ArrayList<User> FileToObject = new ArrayList<>();
Scanner in = null;
boolean err0 = false;
try {
in = new Scanner(Paths.get("User_info.txt"));
} catch (IOException ex) {
err0 = true;
}
if (!err0) // if couldn't open file then skip
{
while (in.hasNext()) {
String theUser = in.nextLine();
String[] Line = theUser.split(",");


User user = null;
try {
if ( Line.length == 10) {
// reconstruct Address
Address a1 = new Address( Line[2], Line[3], Line[4], Line[5], Line[6],
Integer.parseInt( Line[7]));
// reconstruct Customer
user = new Customer( Line[0], Line[1], Line[2], Line[3], Line[4], Line[5], Line[6], Line[7],a1, Line[14]);
FileToObject.add(user);

} else {
return null;
}
} catch (NoSuchElementException ex) {
System.out.println("File improperly formed. Terminating.");
}
}
in.close();

}
// write object back to file.
File f1 = new File(Paths.get("User_info.txt").toUri());
Formatter out = null;
boolean err = false;
try {
out = new Formatter(f1);
} catch (FileNotFoundException ex) {
err = true;
}
if (!err) {
//System.out.println(e3.size());
for (User i : FileToObject) {
out.format("%s%n", i.toString()); // output the card object to cvs format
}
out.close();
}

}

}


我的问题是数组 User( user = new Customer(Line[0], Line[1], Line[2], Line[3], Line[4], Line[ 5)) 是枚举类型(如下面客户类的第一个构造函数中所示),因此我收到错误“不兼容类型,字符串无法转换为 UserType(UserType 是枚举)。

import java.util.ArrayList;

public class Customer extends Billing {

Address customerAddress;

public Customer(String id, String firstName, String lastName, String userName, String password, UserType userType, PermissionType permission, Boolean Status, Address billingAddress, String email, Address customerAddress) {
super(id, firstName, lastName, userName, password, userType, permission, Status, billingAddress, email);

this.customerAddress = customerAddress;
permission = PermissionType.Booking;
userType = UserType.VIP;

setId(id);
setPermission(permission);
setCustomerAddress(billingAddress);
}

public Customer(String id, String firstName, String lastName, String userName, String password, UserType userType, PermissionType permission, Boolean Status, Address billingAddress, String email) {
super(id, firstName, lastName, userName, password, userType, permission, Status, billingAddress, email);


}

public Address getCustomerAddress() {
return customerAddress;
}

public void setCustomerAddress(Address customerAddress) {
this.customerAddress = customerAddress;
}

我和我的导师交谈,他告诉我我需要将字符串转换为枚举。我不确定他的意思,因为他不会进一步解释。他是在谈论字符串数组还是我需要将正在读取的文件中的字符串转换为枚举?我有点迷失所以非常感谢任何帮助

最佳答案

如果UserType是一个enum,您可以使用静态UserType.valueOf()方法将其转换,即:

UserType userType = UserType.valueOf(Line[5]);

现在您可以在以下代码中使用 userType 代替 Line[5]

请注意,Line[5] 必须与任何枚举类型的拼写完全匹配,否则将引发 IllegalArgumentException

关于java - 将字符串转换为枚举以允许读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57840883/

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