I saved some of my ArrayList's to a file and it has following format:
[hotel1, hotel2, hotel3] // ArrayList 1 contents
[hotel5, hotel6] // ArrayList 2 contents
When I am reading, I want to assign for example an ArrayList myList, and I want to add hotel1, hotel2 and hotel3 to myList. Any way I can do that directly? Currently I have a string value that reads next line, it saves brackets. Was looking for another way, so that I can assign each line to an ArrayList < String > object.
public class MyClass1 {
ArrayList<String> myList = new ArrayList<String>();
... // some other code
private void loadUp() throws IOException{
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle("Choose a file to open...");
checker = chooser.showOpenDialog(null);
// if open is clicked
if (checker == JFileChooser.APPROVE_OPTION) {
File inFile = chooser.getSelectedFile();
Scanner in = new Scanner(inFile);
while (in.hasNextLine()) {
// Here want to assign next line to myList
}
in.close();
}
}
splitthe string based on your delimeter, and then write that array into anArrayList