The main goal is to store every line of a paragraph included in .txt file to an ArrayList until we stumble on an empty line (line that only contains the string ""). After storing the whole content of the paragraph into the ArrayList , we store the ArrayList itself to a an ArrayList which stores the paragraphs of the .txt file.
The problem is that after printing the content of the ArrayList after the end of loadParagraphs() it turns out that only the last paragraph of .txt is stored in ArrayList in every single position of it. So , for example, if "This is the content of last paragraph" is the content of the last paragraph of .txt file and the file has a number of 4 paragraphs, then after printing the ArrayList elements I get the result [["This is the content of last paragraph"],["This is the content of last paragraph"],["This is the content of last paragraph"]]. Any ideas?
import java.io.FileNotFoundException;
import java.io.FileInputStream;
import java.util.List; import java.util.ArrayList;
import java.util.Scanner;
public class Pdf {
private List<List<String>> lineblocks1 = new ArrayList<List<String>>();
private String filePath;
private Scanner inputReader = null;
public Pdf(String filePath) {
this.filePath = filePath;
}
public void loadParagraphs() {
List<String> subLineblocks = new ArrayList<String>();
try {
inputReader = new Scanner(new FileInputStream(filePath));
} catch (FileNotFoundException e) {
System.out.println("Error opening file.");
System.exit(0);
}
while (inputReader.hasNextLine()) {
String line = inputReader.nextLine();
if (!line.equals("")) {
subLineblocks.add(line);
} else {
lineblocks1.add(subLineblocks);
subLineblocks.removeAll(subLineblocks);
}
}
}
public void printParagraphs() {
for (List<String> par : lineblocks1) {
System.out.println(par);
}
}
}