如何将值从arraylist设置为数组,拆分它们并在jtextfield中设置拆分文本

时间:2016-12-01 14:36:53

标签: java arrays arraylist split jlist

我已经尝试过这段代码,但jtextfield结果为空,任何帮助将不胜感激。我认为问题出现在那里,而不是手动指定文本文件的位置我使用jfilechooser但不确定。请有人帮助我。

try {
        try {

               UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(doc.class.getName()).log(Level.SEVERE,null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(doc.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(doc.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(doc.class.getName()).log(Level.SEVERE, null, ex);
        }
        JFileChooser chooser = new JFileChooser();
        chooser.showOpenDialog(null);
        File f = chooser.getSelectedFile();
        String filename = f.getAbsolutePath();
        try


        {
            FileReader reader = new FileReader (filename);
            BufferedReader br = new BufferedReader(reader);
            jTextArea1.read(br, null);
            br.close();
            jTextArea1.requestFocus();
        }catch(Exception e){

            }
        in = new BufferedReader(new FileReader(filename));
        String str;
        ArrayList<String> list = new ArrayList<>();
        while((str = in.readLine()) != null){
            list.add(str);

        }
        String[] listArray =list.toArray(new String[list.size()]);
        for(int b=0; b<listArray.length;b++){
            String[] Arra= str.split(" ")
             serial_no.setText(Arra.get(0));
             adm_no.setText(Arra.get(2));

1 个答案:

答案 0 :(得分:0)

第二个BufferedReader - in = new BufferedReader(new FileReader("filename"));不正确,因为文件名是字符串而不是变量。

str包含String[] Arra= str.split(" ")中的最后一行 - 是您想要的,或lst.get(i).split(" ")会获得第i行中的字词