为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕田峪4524236 2023-07-19 16:04:51
我正在编写需要获取文件输入然后将值重建到 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;        }        }    }}我的问题是数组 User( user = new Customer(Line[0], Line[1], Line[2], Line[3], Line[4], Line[5)) 的元素 Line[5]是枚举类型(如下面客户类的第一个构造函数中所示),因此我收到错误“不兼容类型,字符串无法转换为 UserType(UserType 是枚举)。我和我的导师交谈,他告诉我需要将字符串转换为枚举。我不确定他的意思,因为他不会进一步解释。他是在谈论字符串数组还是我需要将正在读取的文件中的字符串转换为枚举?我有点迷失所以非常感谢任何帮助
查看完整描述

3 回答

?
繁星淼淼

TA贡献1775条经验 获得超11个赞

如果UserType是一个enum,你可以用静态UserType.valueOf()方法转换它,即:

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

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

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


查看完整回答
反对 回复 2023-07-19
?
HUX布斯

TA贡献1876条经验 获得超6个赞

如果UserType.valueOf无法提供您需要的内容,请添加返回特定实例的静态方法UserType。例如:-


public enum UserType{


    CUSTOMER("customer");


    private String name;


    UserType(String name){

        this.name = name;

    }


    //A Map that holds user type name as key.

    private static Map<String,UserType> userTypeMap = new HashMap<>();


    //Populate userTypeMap with UserType instance

    static{

        for(UserType type : values()){

            userTypeMap.put(type.name, type);

        }

    }


    public static UserType of(String name){


        return userTypeMap.get(name);

    }

}

调用者可以获得UserType如下实例:-


UserType type = UserType.of("customer");


查看完整回答
反对 回复 2023-07-19
?
FFIVE

TA贡献1797条经验 获得超6个赞

你的解决方案太长了。有一种优雅的方法可以做到这一点


Properties properties = new Properties();

properties.load(new FileInputStream(new File("FileLocation")));

properties.values();

文件可以包含值列表、键值对。在第 2 行,Properties 对象中加载了第 2 个文件。现在您可以根据您的需要进行处理。


查看完整回答
反对 回复 2023-07-19
  • 3 回答
  • 0 关注
  • 101 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信