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

我正在编写需要获取文件输入然后将值重建到 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 是枚举)。

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


慕田峪4524236
浏览 104回答 3
3回答

繁星淼淼

如果UserType是一个enum,你可以用静态UserType.valueOf()方法转换它,即:UserType&nbsp;userType&nbsp;=&nbsp;UserType.valueOf(Line[5]);现在您可以在以下代码中使用userType来代替。Line[5]请注意,Line[5]必须与任何枚举类型的拼写完全匹配,否则IllegalArgumentException将抛出异常。

HUX布斯

如果UserType.valueOf无法提供您需要的内容,请添加返回特定实例的静态方法UserType。例如:-public enum UserType{&nbsp; &nbsp; CUSTOMER("customer");&nbsp; &nbsp; private String name;&nbsp; &nbsp; UserType(String name){&nbsp; &nbsp; &nbsp; &nbsp; this.name = name;&nbsp; &nbsp; }&nbsp; &nbsp; //A Map that holds user type name as key.&nbsp; &nbsp; private static Map<String,UserType> userTypeMap = new HashMap<>();&nbsp; &nbsp; //Populate userTypeMap with UserType instance&nbsp; &nbsp; static{&nbsp; &nbsp; &nbsp; &nbsp; for(UserType type : values()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; userTypeMap.put(type.name, type);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public static UserType of(String name){&nbsp; &nbsp; &nbsp; &nbsp; return userTypeMap.get(name);&nbsp; &nbsp; }}调用者可以获得UserType如下实例:-UserType type = UserType.of("customer");

FFIVE

你的解决方案太长了。有一种优雅的方法可以做到这一点Properties properties = new Properties();properties.load(new FileInputStream(new File("FileLocation")));properties.values();文件可以包含值列表、键值对。在第 2 行,Properties 对象中加载了第 2 个文件。现在您可以根据您的需要进行处理。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java