亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將字符串轉換為枚舉以允許讀取文件

將字符串轉換為枚舉以允許讀取文件

慕田峪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 個文件?,F在您可以根據您的需要進行處理。


查看完整回答
反對 回復 2023-07-19
  • 3 回答
  • 0 關注
  • 199 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號