Подскажите пожалуйста, что неправильно в моем коде
Мне нужно сделать поверку на количество пробелов в строке word документа. Что я делаю неправильно и почему не выводится текст в word?
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
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);
String[] nmbArray = {};
ArrayList<String> numberandnames = new ArrayList<>((Arrays.asList(nmbArray)));
send.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
numberandnames.add(name_field.getText());
numberandnames.add(number_field.getText());
try {
File file = new File("C:\\Users\\Admin\\Desktop\\Anketa.docx");
file.createNewFile();
Path link = Paths.get("C:\\Users\\Admin\\Desktop\\Anketa.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();
String word = name_field.toString();
String data[];int k=0;
data=word.split("");
for(int i=0;i<data.length;i++) {
if (data[i].equals(" ")) {
k++;
}
if (k >= 1) {
for (XWPFParagraph para : paragraphs) {
paragraphConfig.setText(numberandnames.toString());
break;
}
}
break;
}
fis.close();
try {
FileOutputStream outputStream = new FileOutputStream("C:\\Users\\Admin\\Desktop\\Anketa.docx");
docxModel.write(outputStream);
outputStream.close();
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
} catch (IOException ex) {
throw new RuntimeException(ex);
}
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\\Anketa.docx"));
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
dispose();
}
});
// Создание панели содержимого с размещением кнопок
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;
}
}
}