Необходимо перенести элементы из Интернета в arraylist - PullRequest
0 голосов
/ 24 января 2012

После успешной очистки веб-страниц с соответствующими данными мне нужно перенести их на ArrayList.

. Как вы можете видеть ниже, я распечатал данные.Теперь все, что мне нужно сделать, это поместить в ArrayList.Есть идеи?

Код:

static String html = "http://games.espn.go.com/fba/playerrater?&slotCategoryId=1";

public static void main(String[] args) throws IOException, SQLException, InterruptedException {
    Document doc = Jsoup.connect(html).get();
    String title = doc.title();
   System.out.println(title);


    Iterator<Element> trSIter = doc.select("table.playerTableTable").iterator();
    while (trSIter.hasNext()) {
        Element  trEl = trSIter.next().child(0);
        Elements  tdEls = trEl.children();
        Iterator<Element> tdIter = tdEls.select("tr").iterator();
        boolean firstRow = true;
        while (tdIter.hasNext()) {

            Element tr = (Element)tdIter.next();
            if (firstRow) {
                firstRow = false;
                continue;
            }

        while (tdIter.hasNext()) 
        {  

            System.out.println("============================");
            Element tdEl = tdIter.next();
            String name = 
                    tdEl.getElementsByClass("playertablePlayerName").text();
            System.out.println("Name: "+name);
            String pointStr = 
                    tdEl.getElementsByClass("sortedCell").text();
            System.out.println("points: "+pointStr);
            //System.out.println(tdEl);
            Elements tdsEls = tdEl.select("td.playertableData");
            Iterator<Element> columnIt = tdsEls.iterator();
            boolean firstRow1 = true;


            while(columnIt.hasNext())
            {


                Element column = columnIt.next();
                 String stat1 =column.text();
                System.out.print(column.className()+":"+column.text()+","); 

ВЫХОД:

Name: Kobe Bryant, LAL SG
points: 14.68
playertableData:1,playertableData:0.62,playertableData:2.37,playertableData:1.36,playertableData:1.20,playertableData:2.66,playertableData:1.73,playertableData:0.15,playertableData:4.59,playertableData sortedCell:14.68,============================
Name: Joe Johnson, Atl SG, SF
points: 10.04
playertableData:2,playertableData:-0.58,playertableData:2.05,playertableData:3.27,playertableData:0.46,playertableData:1.41,playertableData:0.97,playertableData:0.01,playertableData:2.44,playertableData sortedCell:10.04,============================
Name: James Harden, OKC SG
points: 9.28
playertableData:3,playertableData:0.73,playertableData:2.48,playertableData:2.53,playertableData:0.53,playertableData:1.00,playertableData:0.46,playertableData:-0.25,playertableData:1.81,playertableData sortedCell:9.28,============================
Name: Andre Iguodala, Phi SF, SG
points: 8.74
playertableData:4,playertableData:0.42,playertableData:-1.47,playertableData:1.57,playertableData:1.31,playertableData:1.90,playertableData:3.13,playertableData:0.68,playertableData:1.19,playertableData sortedCell:8.74,============================
Name: Monta Ellis, GS PG, SG
points: 8.38
playertableData:5,playertableData:-0.58,playertableData:0.77,playertableData:1.15,playertableData:-0.14,playertableData:2.73,playertableData:2.11,playertableData:-0.12,playertableData:2.46,playertableData sortedCell:8.38,============================
Name: Kevin Martin, Hou SG
points: 7.87
playertableData:6,playertableData:-0.91,playertableData:3.01,playertableData:2.85,playertableData:0.02,playertableData:0.79,playertableData:0.46,playertableData:-0.66,playertableData:2.31,playertableData sortedCell:7.87,============================
Name: Jarrett Jack, Nor PG, SG
points: 7.67
playertableData:7,playertableData:0.15,playertableData:1.49,playertableData:0.51,playertableData:0.44,playertableData:3.25,playertableData:0.33,playertableData:-0.12,playertableData:1.62,playertableData sortedCell:7.67,============================
Name: Jason Terry, Dal SG
points: 7.31
playertableData:8,playertableData:-0.89,playertableData:0.77,playertableData:3.27,playertableData:-0.53,playertableData:1.41,playertableData:2.24,playertableData:-0.39,playertableData:1.42,playertableData sortedCell:7.31,============================
Name: Luke Ridnour, Min PG, SG
points: 6.21
playertableData:9,playertableData:1.05,playertableData:0.40,playertableData:1.57,playertableData:-0.37,playertableData:1.38,playertableData:1.22,playertableData:-0.12,playertableData:1.08,playertableData sortedCell:6.21,============================
Name: Ray Allen, Bos SG  DTD
points: 6.19
playertableData:10,playertableData:1.21,playertableData:1.35,playertableData:3.06,playertableData:-0.16,playertableData:0.31,playertableData:0.20,playertableData:-0.79,playertableData:1.01,playertableData sortedCell:6.19,============================
Name: Tyreke Evans, Sac PG, SG
points: 6.05
playertableData:11,playertableData:-1.04,playertableData:0.05,playertableData:0.19,playertableData:0.94,playertableData:1.90,playertableData:1.98,playertableData:0.15,playertableData:1.87,playertableData sortedCell:6.05,============================
Name: D.J. Augustin, Cha PG, SG  DTD
points: 5.88
playertableData:12,playertableData:-0.93,playertableData:0.73,playertableData:2.00,playertableData:0.07,playertableData:3.07,playertableData:0.33,playertableData:-0.66,playertableData:1.26,playertableData sortedCell:5.88,============================
Name: Wesley Matthews, Por SG, SF
points: 5.43
playertableData:13,playertableData:-0.80,playertableData:1.14,playertableData:2.32,playertableData:0.14,playertableData:-0.04,playertableData:1.86,playertableData:-0.39,playertableData:1.21,playertableData sortedCell:5.43,============================
Name: Brandon Knight, Det PG, SG
points: 5.38
playertableData:14,playertableData:-0.31,playertableData:0.46,playertableData:2.10,playertableData:0.30,playertableData:1.17,playertableData:0.84,playertableData:-0.39,playertableData:1.20,playertableData sortedCell:5.38,============================
Name: Paul George, Ind SF, SG
points: 5.37
playertableData:15,playertableData:0.04,playertableData:0.58,playertableData:1.79,playertableData:0.64,playertableData:0.38,playertableData:1.09,playertableData:0.28,playertableData:0.57,playertableData sortedCell:5.37,============================
Name: MarShon Brooks, NJ SG, SF  DTD
points: 5.22
playertableData:16,playertableData:0.67,playertableData:0.34,playertableData:1.26,playertableData:0.53,playertableData:0.03,playertableData:0.97,playertableData:0.01,playertableData:1.41,playertableData sortedCell:5.22,============================
Name: Paul Pierce, Bos SF, SG
points: 5.20

Ответы [ 2 ]

0 голосов
/ 25 января 2012

Спасибо, Уоррен!

Я добавил переключатель, чтобы пройти через все элементы. Надеюсь, что некоторые люди могут выиграть от моего вопроса. Спасибо за ваше время.

switch (tdCount++) {

                    case 1:
                        stat1 = Double.parseDouble(column.text());
                        break;
                    case 2:
                        stat2 = Double.parseDouble(column.text());
                        break;
                    case 3:
                        stat3 = Double.parseDouble(column.text());
                        break;
                    case 4:
                        stat4 = Double.parseDouble(column.text());
                        break;
                    case 5:
                        stat5 = Double.parseDouble(column.text());
                        break;
                    case 6:
                        stat6 = Double.parseDouble(column.text());
                        break;
                    case 7:
                        stat7 = Double.parseDouble(column.text());
                        break;
                    case 8:
                        stat8 = Double.parseDouble(column.text());
                        break;
                    case 9:
                        stat9 = Double.parseDouble(column.text());
                        break;
                    case 10:
                        stat10 = Double.parseDouble(column.text());
                        break;

                    }
                }

                PlayerBean pb = new PlayerBean(name, stat1, stat2, stat3,
                        stat4, stat5, stat6, stat7, stat8, stat9, stat10);
                playerlist.add(pb);
            }
0 голосов
/ 24 января 2012

Создайте класс Player, содержащий переменные для имени, очков и т. Д. Затем создайте объект для каждого игрока и, наконец, добавьте его в ArrayList<Player>.

Player currentPlayer;
ArrayList<Player> players = new ArrayList<Player>();
// a lot of your code
while (tdIter.hasNext()) {  
    System.out.println("============================");
    Element tdEl = tdIter.next();
    currentPlayer = new Player();
    // instead of that:
    //String name = tdEl.getElementsByClass("playertablePlayerName").text();
    // do that:
    currentPlayer.mName = tdEl.getElementsByClass("playertablePlayerName").text();
    // the same with the rest of the gathered information
    // at the last line in this while loop
    players.add(currentPlayer);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...