0

I can not get this code to work. The .txt file that it is reading from is here: urls.txt

The problem with this code is that it does not at all add any lines. Whenever i try to get anything at all from "lists" it gives me an IndexOutOfBoundsException.

The code gets executed each 30 seconds, so i have to call the lists.clear(); method.

NOTE: "lists" is defined earlier:

    public static ArrayList<ArrayList<String>> lists = new ArrayList<ArrayList<String>>();

This is my code:

                try {
                URL urls = new URL("https://dl.dropboxusercontent.com/u/22001728/server%20creator/urls.txt");
                BufferedReader br = new BufferedReader(new InputStreamReader(urls.openStream()));

                lists.clear();

                String line;
                ArrayList<String> list = new ArrayList<String>();

                lists.add(list);

                int y = 0, z = 0;

                while ((line = br.readLine()) != null) {
                    y++;
                    if(line.equalsIgnoreCase(">")) {
                        System.out.print("Received command: INSERT ");
                        lists.add(list);
                        list.clear();
                        z++;
                        System.out.print(" ;  Jumping to " + z + "\n");
                    } else {
                        System.out.println("Line: " + line + " added to lists[" + z + "].");
                        lists.get(z).add(line);
                    }
                }

                int selectedIndex = comboModel0.getIndexOf(comboModel0.getSelectedItem());
                comboModel0.removeAllElements();

                System.out.println("\n\n\n");

                System.out.println(lists.get(1).get(1));

                System.out.println("\n\n\n");

            } catch (IOException e) {
                System.out.println("Failed to download URLS data.");
            }
1
  • Side note: Why are you doing line.equalsIgnoreCase(">")? Since when > can be uppercase or lowercase? Commented May 18, 2013 at 17:25

1 Answer 1

3

You are adding a list to your collection of lists, then clearing it. That will clear your local variable, plus the list in your collection.

lists.add(list);
list.clear();

You need to create a copy of that list to add to your collection.

lists.add(new ArrayList<String>(list));
Sign up to request clarification or add additional context in comments.

5 Comments

Or just create a new ArrayList object. 1+
The OP never adds items to a new list anyway. He needs to populate it somehow. There are other stylistic problems: lists should be a List<List<String>>, for example.
Yeah, i tried that, but List wouldn't work: The type List is not generic; it cannot be parameterized with arguments <String>
Huh? Are you using the right List? Make sure you haven't imported java.awt.List by mistake.
@9903286 yep, you were using the wrong List likely. You need to use java.util.List.

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.