I would need to create java bean class from strings which are read from .docx file. The docx file looks like:
Comments:
20.6.2018 16:18-16:25 problem: first problem, action first action
20.6.2018 16:20-16:45 problem: second problem, action: second action
20.6.2018 16:25-16:30 problem: third problem, action: third action
Check list based on data from 24.6.2018.
I created a CheckList Class to read the docx where I used the instance of FAData.class POJO to set its instant variables according to How to parse a csv file and store datails in java bean class
public class CheckList {
String Check;
ArrayList<String> ActionG = new ArrayList<String>();
ArrayList<String> ProblemG = new ArrayList<String>();
ArrayList<String> DateG = new ArrayList<String>();
ArrayList<String> BeginTimeG = new ArrayList<String>();
ArrayList<String> EndTimeG = new ArrayList<String>();
public void readDocxFile(String fileName) {
FAData data = new FAData(); // instance of FAData POJO
try {
File file = new File(fileName);
FileInputStream fis = new FileInputStream(file.getAbsolutePath());
XWPFDocument document = new XWPFDocument(fis);
List<XWPFParagraph> paragraphs = document.getParagraphs();
String Comments = "";
for (XWPFParagraph para : paragraphs) {
Comments = Comments.concat(para.getText() + "\n");
}
Check = Comments.substring(Comments.lastIndexOf("Check"), Comments.length()); // split the last sentence
String CommentsG = Comments.substring(Comments.indexOf(":") + 1, Comments.lastIndexOf("Check")); // split the sentences between „Comments:” and the last sentence
data.setCheck(Check); // add the Check instant variable to FAData POJO
if (!CommentsG.equals("")) { // check if sentences exist between „Comments:” and the last sentence
// add the substrings to ArrayLists
String[] FAG = CommentsG.split("\n");
for (int i = 1; i < FAG.length; i++) {
ActionG.add(FAG[i].substring(FAG[i].indexOf("action") + 7, FAG[i].length()));
ProblemG.add(FAG[i].substring(FAG[i].indexOf("problem") + 9, FAG[i].indexOf("action") - 2));
DateG.add(FAG[i].substring(0, 10));
BeginTimeG.add(FAG[i].substring(FAG[i].indexOf(":") - 2, FAG[i].indexOf("-")));
EndTimeG.add(FAG[i].substring(FAG[i].indexOf("-") + 1, FAG[i].indexOf("-") + 6));
}
}
// add the ArrayList instant variables to FAData POJO
data.setActionG(ActionG);
data.setProblemG(ProblemG);
data.setDateG(DateG);
data.setBeginTimeG(BeginTimeG);
data.setEndTimeG(EndTimeG);
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class FAData {
String Check;
ArrayList<String> ActionG = new ArrayList<String>();
ArrayList<String> ProblemG = new ArrayList<String>();
ArrayList<String> DateG = new ArrayList<String>();
ArrayList<String> BeginTimeG = new ArrayList<String>();
ArrayList<String> EndTimeG = new ArrayList<String>();
public String getCheck() {
return Check;
}
public void setCheck(String Check) {
this.Check = Check;
}
public ArrayList<String> getActionG() {
return ActionG;
}
public void setActionG(ArrayList<String> ActionG) {
this.ActionG = ActionG;
}
....
//Getter and Setter for the rest variables
}
I tested if the variables are available from FAData.class:
public class test {
public static void main(String[] args) {
CheckList TC = new CheckList();
String fileName = "c:\\path\\to\\docx";
try {
TC.readDocxFile(fileName);
FAData datas = new FAData();
String testCheck = datas.getCheck();
ArrayList<String> testDate = datas.getDateG();
System.out.println(testCheck);
System.out.println(testDate);
} catch (Exception e) {
System.out.println(e);
}
}
}
but I got null values. I do not know what I did wrong or how I should thest FAData.class. Could someone give me a suggestion?
Meantime I figured out the solution. I added the List<FAData> datas = new ArrayList<FAData>();
inside readDocxFile
method and added to it the data
values:
datas.add(data);
If I create a new instant of CheckList object in an other class I can get the data e.g:
CheckList TC = new CheckList();
List<FAData> D = TC.readDocxFile(fileName);
String DateG = D.get(0).getDateG(0);