Нужна помощь. Необходимо убрать повторный вывод данных из списка

public class Main {
    public static void main(String[] args) throws IOException {
        contactForm form = new contactForm();
        form.setVisible(true);
    }
}


public class contactForm extends JFrame {

    JTextField name_field, number_field;

    public contactForm() {

        super("Анкета");
        super.setBounds(250, 250, 600, 200);
        super.setDefaultCloseOperation(EXIT_ON_CLOSE);

        Container container = super.getContentPane();
        container.setLayout(new GridLayout(5, 2, 2, 10));

        JLabel name = new JLabel("ФИО");
        name_field = new JTextField("");
        JLabel number = new JLabel("Номер телефона: ");
        number_field = new JTextField("+");

        container.add(name);
        container.add(name_field);
        container.add(number);
        container.add(number_field);

        JButton send = new JButton("Добавить контакт");

        container.add(send);

        send.addActionListener(new ButtonEventManager());

    }


    class ButtonEventManager implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {

            String[] nmbArray = {};
            ArrayList<String> numberandnames = new ArrayList<>((Arrays.asList(nmbArray)));
            numberandnames.add(name_field.getText());
            numberandnames.add(number_field.getText());


            try {
                File file = new File("C:\\Users\\Admin\\Desktop\\BBB.docx");
                Path link = Paths.get("C:\\Users\\Admin\\Desktop\\BBB.docx");
                if (Files.exists(link)) {
                    FileInputStream fis = new FileInputStream(file.getAbsolutePath());
                    XWPFDocument docxModel = new XWPFDocument(fis);
                    docxModel.createParagraph();
                    String documentLine = docxModel.getDocument().toString();
                    CTSectPr ctSectPr = docxModel.getDocument().getBody().addNewSectPr();
                    XWPFParagraph bodyParagraph = docxModel.createParagraph();
                    bodyParagraph.setAlignment(ParagraphAlignment.LEFT);
                    XWPFRun paragraphConfig = bodyParagraph.createRun();
                    XWPFParagraph paragraph = docxModel.createParagraph();
                    paragraphConfig.setItalic(true);
                    paragraphConfig.setFontSize(20);
                    paragraphConfig.setColor("170101");
                    paragraphConfig.setFontSize(12);
                    List<XWPFParagraph> paragraphs = docxModel.getParagraphs();
                    for (XWPFParagraph para : paragraphs) {
                        paragraphConfig.setText(numberandnames.toString());;
                    }
                    fis.close();
                    try{
                        FileOutputStream outputStream = new FileOutputStream("C:\\Users\\Admin\\Desktop\\BBB.docx");
                        docxModel.write(outputStream);
                        outputStream.close();
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                }
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            dispose();
            contactForm.Word wrd = new contactForm.Word();
            wrd.setVisible(true);
        }
    }



    static class Word extends JFrame {
        public Word() {
            super("Анкета");
            // Выход из программы при закрытии
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            // Кнопки для создания диалоговых окон
            JButton docx = new JButton(".docx");
            docx.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    Desktop desktop = null;
                    if (Desktop.isDesktopSupported()) {
                        desktop = Desktop.getDesktop();
                        try {
                            desktop.open(new File("C:\\Users\\Admin\\Desktop\\BBB.docx"));
                        } catch (IOException ioe) {
                            ioe.printStackTrace();
                        }
                    }

                }

            });

            // Создание панели содержимого с размещением кнопок
            JPanel contents = new JPanel();
            contents.add(docx);
            setContentPane(contents);
            // Определение размера и открытие окна
            setSize(350, 100);
        }

        private JDialog createDialog(String title, boolean modal) {
            final JDialog dialog = new JDialog(this, title, modal);
            dialog.setDefaultCloseOperation(EXIT_ON_CLOSE);
            dialog.setSize(180, 90);
            return dialog;
        }
    }
}

введите сюда описание изображения

Догадывался, что это из-за применения for-each вместе с массивом в списке. Подскажите пожалуйста


Ответы (0 шт):