Lostsoul
Lostsoul

Reputation: 25999

is there a way to skip the first entry in an iterator?

I have some java code that takes a html table and turns it into an Iterator that I use a while loop to parse and add to a database. My problem is the header of the table is causing me problems while I am going through my while look(since its not passing my data quality checks). Is there a way to skip the first row?

Iterator HoldingsTableRows = HoldingsTableRows.iterator();


    while (HoldingsTableRows.hasNext()) {

}

I could get the contents of a variable and if it matches I can break out of the loop but I'm trying to avoid hard coding anything specific to the header names because if the names of the headers change it would break my app.

please help!

Thanks!

Upvotes: 13

Views: 23160

Answers (2)

Tom Neyland
Tom Neyland

Reputation: 6968

All you need to do is call .next() once before you begin your while loop.

Iterator HoldingsTableRows = HoldingsTableRows.iterator();

//This if statement prevents an exception from being thrown
//because of an invalid call to .next()
if (HoldingsTableRows.hasNext())
    HoldingsTableRows.next();

while (HoldingsTableRows.hasNext())
{
    //...somecodehere...
}

Upvotes: 36

digitaljoel
digitaljoel

Reputation: 26574

Call next() once to discard the first row.

Iterator HoldingsTableRows = HoldingsTable.iterator();

    // discard headers
    HoldingsTableRows.next();
    // now iterate through the rest.
    while (HoldingsTableRows.hasNext()) {

}

Upvotes: 3

Related Questions