0

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();
}
 }
5
  • Do you need this format or could you use JSON? Commented Apr 4, 2013 at 17:58
  • So you mean you want to get an ArrayList printed to a file from the toString() method back into a real ArrayList from the string? Commented Apr 4, 2013 at 18:00
  • All I want is, when I'm reading the file, each line is assigned to an ArrayList. But I want each word to be like a new element, for an ArrayList. Commented Apr 4, 2013 at 18:01
  • 1
    So what have you tried? The logical steps are to split the string based on your delimeter, and then write that array into an ArrayList Commented Apr 4, 2013 at 18:03
  • Was looking for a more direct way, if there was one. I could always do the split method you've mentioned. Commented Apr 4, 2013 at 18:07

2 Answers 2

3

Use this:

Scanner file = new Scanner(myFile);
ArrayList<Scanner> lines = new ArrayList<>();
while(file.hasNextLine())
    lines.add(new Scanner(file.nextLine()));
ArrayList<ArrayList<String>> lists = new ArrayList<>(lines.size());
for(Scanner s : lines)
    s.useDelimeter("[, ]" + System.lineSeparator());
for(int i = 0; i < lines.size(); ++i)
{
    while(lines.get(i).hasNext())
    {
        lists.add(new ArrayList<String>());
        lists.get(i).add(lines.get(i).next());
    }
}

Then you'll end up with a list of lists of strings that contain the values.

Sign up to request clarification or add additional context in comments.

2 Comments

@Mr Heisenberg but do you understand how and why the code works? I don't intend my code samples to be taken for granted. Also, if it works, you can accept the answer and it will become the main answer for the question (for anyone looking).
At the end, I think I got what originally had. I still gotta split string and then add each item individually. Thanks for answer though!
2

Try below code to read each line

 BufferedReader br = new BufferedReader(new FileReader(file));
    String line;
    while ((line = br.readLine()) != null) {
       // process the line.
    }
    br.close();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.