I have a text file which contains multiple titles and descriptive text below each title. I am trying to add each title to a hashmap key and the descriptive text to the value but cannot figure out how to iterate over this text file and properly parse the information. The title will ALWAYS contain an empty line before and after the title (unless it is the first line) and the value will ALWAYS be the text immediately after the respective title UNTIL the next title is reached in the file. The user will be given a list of keys to choose from in the program and the corresponding value will be printed.
Example Text File
Title
text
text
text
text
text
Title
text
text
text
text
The text file has multiple titles in the file and must be a single file.
Code
private static HashMap<String, ArrayList<String>> hashmap = new HashMap<>();
public static void processTextFile(ArrayList array) {
//Place first line in hashmap key
hashmap.put((String) array.get(0), new ArrayList<>());
//Iterate over text file and add titles which have null spaces before and after as a key
for (int i = 0; i < array.size(); i++) {
if (array.get(i).equals("") && array.get(i + 2).equals("")) {
hashmap.put((String) array.get(i + 1), new ArrayList<>());
}
}
}
Desired Result
Please select a Title:
*user types a specific title*
Title
text
text
text
text
text