Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the index in an ArrayList that contains a string

By using Jsoup I parse HTML from a website to populate an ArrayList with what I needed to fetch from the website. So now I have an ArrayList that is filled with strings. I want to find the index in that list that contains a certain string. For example, I know that somewhere in the list, in some index, there is the string(literal) "Claude" but I can't seem to make any code that finds the index that contains "Claude" in the ArrayList... here is what I have tried but returns -1 (not found):

ArrayList < String > list = new ArrayList < String > ();
String claude = "Claude";

Document doc = null;
try {
    doc = Jsoup.connect("http://espn.go.com/nhl/team/stats/_/name/phi/philadelphia-flyers").get();
} catch (IOException e) {
    e.printStackTrace();
}
for (Element table: doc.select("table.tablehead")) {
    for (Element row: table.select("tr")) {
        Elements tds = row.select("td");
        if (tds.size() > 6) {
            String a = tds.get(0).text() + tds.get(1).text() + tds.get(2).text() + tds.get(3).text() + tds.get(4).text() + tds.get(5).text() + tds.get(6).text();

            list.add(a);

            int claudesPos = list.indexOf(claude);
            System.out.println(claudesPos);
        }
    }
}
like image 608
MaxK Avatar asked Feb 20 '13 06:02

MaxK


People also ask

How do you find the index of an element in an ArrayList?

The index of a particular element in an ArrayList can be obtained by using the method java. util. ArrayList. indexOf().

Can you take the index of a string in Java?

You can get the character at a particular index within a string by invoking the charAt() accessor method. The index of the first character is 0, while the index of the last character is length()-1 . For example, the following code gets the character at index 9 in a string: String anotherPalindrome = "Niagara.

Can indexOf be used for ArrayList?

The indexOf() method is used to get the index of the first occurrence of an element in an ArrayList object. The element whose index is to be returned. Return Value: The index of the first occurrence an element in ArrayList, or -1 if the element does not exist.

How do you check if an element is present in the index in the ArrayList in Java?

ArrayList. contains() method can be used to check if an element exists in an ArrayList or not. This method has a single parameter i.e. the element whose presence in the ArrayList is tested. Also it returns true if the element is present in the ArrayList and false if the element is not present.


1 Answers

You're confusing String.indexOf and List.indexOf. Considering the following list:

list[0] = "Alpha Bravo Charlie"
list[1] = "Delta Echo Foxtrot"
list[2] = "Golf Hotel India"

list.indexOf("Foxtrot") => -1
list.indexOf("Golf Hotel India") => 2
list.get(1).indexOf("Foxtrot") => 11

So:

if (tds.size() > 6) {
  // now the string a contains the text of all of the table cells joined together
  String a = tds.get(0).text() + tds.get(1).text() + tds.get(2).text() +
      tds.get(3).text() + tds.get(4).text() + tds.get(5).text() + tds.get(6).text();

  // now the list contains the string
  list.add(a);

  // now you're looking in the list (which has all the table cells' items)
  // for just the string "Claude", which doesn't exist
  int claudesPos = list.indexOf(claude);
  System.out.println(claudesPos);

  // but this might give you the position of "Claude" within the string you built
  System.out.println(a.indexOf(claude));
}

for (int i = 0; i < list.size(); i += 1) {
  if (list.get(i).indexOf(claude) != -1) {
    // list.get(i).contains(claude) works too
    // and this will give you the index of the string containing Claude
    // (but not the position within that string)
    System.out.println(i);
  }
}
like image 138
Jeff Bowman Avatar answered Oct 18 '22 23:10

Jeff Bowman