我正在尝试将拆分文本文件中的数据保存到JComboBox

时间:2018-12-08 13:16:38

标签: java eclipse split bufferedreader jcombobox

文本文件具有这样的结构

  

1.5美元,$

     

英镑,1英镑

我需要将每行的第一部分(例如Dollars和Pounds)保存到ComboBox中,目前我的代码仅将文件中最后一行的开头保存。我认为这是因为我正在覆盖它,但我不知道如何避免它。

移动“ cmbCurrency = new JComboBox(list);”在循环之外意味着我无法访问StringArray(列表)。

try {
    // instantiate BufferedReader object
    BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("currency.txt"), "UTF8"));
    String line = in.readLine();

    //ArrayList<String> list = new ArrayList<String>();
    while((line !=null)){
        String [] parts = line.split(","); //Segments each line by each comma
        String one = parts[0];
        String two = parts[1];
        String three = parts[2];
        String [] list = {one};
        //String currency = new String(one);
        line = in.readLine(); //Read next available line
        System.out.println(one);    //Printing out to see if text file is being read 
        System.out.println(three);
        cmbCurrency = new JComboBox<String>(list);
    }
    in.close();
} catch (Exception e) {
    // Something went wrong.
    String msg = e.getMessage();
    // show the message to the user!
}

感谢您的帮助。

0 个答案:

没有答案