0

Please help me. I can't get more then one value from my ArrayList. Trying to parse some page, get all data. Put it inside ArrayList, but when I try to print it (by using get(0)), I can print only one value, when try get(1), program has crashed with

java.lang.IndexOutOfBoundsException: Index: 2, Size: 1

But I know that the Arraylist is more then one value. Here is my code.

public class jaja {
    final int[] score = {0};
    public static void main (String[] args) throws IOException {

        List<Integer> quesList = new ArrayList<>();
        List<String> f1 = new ArrayList<>();
        Document doc = Jsoup.connect("http://somepage.com/index/").get();

        org.jsoup.select.Elements superelements = doc.getElementsByTag("tr");

        for(Element Element : superelements) {
            String Test = Element.getElementsByTag("td").get(0).text();
            String Result = Element.getElementsByTag("td").get(1).text();
            String fd = Element.getElementsByTag("td").get(1).text();
            f1.add(fd);
            Collections.shuffle(f1);


            System.out.println("(" + "\"" + Test + "\"" + "," + "\"" + f1+ "\"" + "," + "\"" + f1.get(0) + "\"" + "," + "\"" + f1.get(f1.size() - 1) + "\"" + "," + "\"" + f1.get(f1.size() - 1) + "\"" + "," + "\"" + f1.get(f1.size() - 1) + "\"" + "," + "\"" + f1.get(f1.size() - 1) + "\"" + ");");
        }
    }
}
Arsen Davtyan
  • 1,891
  • 8
  • 23
  • 40
Petro
  • 1
  • 4
  • 1
    What makes you so sure that the list contains more than one value? The exception specifically indicates that's not the case. – Matt Ball Nov 30 '16 at 01:09
  • Try print the length of the ArrayList instead? – shole Nov 30 '16 at 01:10
  • @MattBall when I print without .get , I get all values inside ArrayList . – Petro Nov 30 '16 at 01:35
  • 1
    @shole do you mean .size? I try it , but it doesn't work :( – Petro Nov 30 '16 at 01:38
  • @Peter I don't really know the method in JAVA but yea, just print the length of the ArrayList, and is it more than 1? Also what will you do if f1.size() - 1 < 0? – shole Nov 30 '16 at 02:14
  • To resolve in the for statment add this line: if (Element.childNodeSize()>0) { String Test = Element.getElementsByTag("td").get(0).text(); } – toto Nov 30 '16 at 02:14
  • @shole Ye, it's more than 1. f1.size() - 1, same as f1.get(0) – Petro Nov 30 '16 at 02:28
  • @toto doesn't work for me ;( – Petro Nov 30 '16 at 02:28
  • Element.getElementsByTag("td").get(1) <--- You also sure there is always more than 1 td element? Have you printed the size of Element.getElementsByTag("td")? – shole Nov 30 '16 at 02:35
  • The problem is that TAG TR is a child of TABLE. if you search all tr not alway Element.getElementsByTag("td").get(1) exist in the current table. Try example this: org.jsoup.select.Elements superelements = doc.select("table").eq(0).select("tr"); – toto Nov 30 '16 at 02:37

0 Answers0