Jack
Jack

Reputation: 10613

Java enum and loop question

I have a list of Nodes which I loop through:

for (int i = 0; i < aNodes.getLength(); i++) {
  //
}

For example, assume the list contains 12 items, and that I know 3 items represent a row in a table, and the next 3 belong to the following row. That means my list of 12 Nodes originates from an HTML table containing 4 rows.

After each row I want to do something, for example, create a new object and populate it...or whatever. I have a solution:

ArrayList<Account> mAccounts = new ArrayList<Account>();
Account account = new Account();

for (int i = 0; i < aNodes.getLength(); i++) {

            String nodeValue = aNodes.item(i).getNodeValue();
            boolean isNewRow = (i % COLS_PER_ROW == 0);

            if (isNewRow) {
                account = new Account();
                mAccounts.add(account);
            }

            switch (i % COLS_PER_ROW) {
            case ACCOUNT_POS:
                account.setAccount(nodeValue);
                break;
            case BALANCE_POS:
                account.setBalance(nodeValue);
                break;
            case DATE_POS:
                account.setDate(nodeValue);
                break;
            }
        }

But there are numerous things I don't like about this solution:

  1. An Account instance is created twice first time, once outside the loop and then once because a new row is detected.
  2. It uses integer constants ACCOUNT_POS=0, BALANCE_POS=1, DATE_POS=2...this doesn't feel very nice and I think I should be using an enum.
  3. I can't use an enum with the loop variable 'i'.
  4. I can't use a for each loop since Nodes doesn't implement the correct interface,

Can anyone suggest a better way of doing this that solves the list of things I don't like about it?

Thank you.

Upvotes: 0

Views: 276

Answers (3)

Marco Bizzarri
Marco Bizzarri

Reputation: 371

You're working with a position based file, so, if I understand you correctly, you've a structure which is organized as follows:

  • account;
  • balance,
  • date;

Knowing this, maybe you could better deal with the index in a while loop, rather than using a for loop, as follows:

int i = 0;
while (i < aNodes.getLength()) {
   account = new Account();
   account.setAccount(aNodes.item(i).getNodeValue());
   account.setBalance(aNodes.item(i+1).getNodeValue());
   account.setBalance(aNodes.item(i+2).getNodeValue());
   mAccounts.add(account)
   i += 3;
}

I would also suggest to extract the account-filling-code in a new method, like 'extractAccountFromNodes', and then call it inside the loop; so the loop could become:

int i = 0;
while (i < aNodes.getLength()) {
   mAccount.add(extractAccountFromNodes(aNodes, i));
   i += 3;
}

with the method extractAccountFromNodes like this:

private Account extractAccountFromNodes(Nodes nodes, int position) {
   account = new Account();
   account.setAccount(nodes.item(i).getNodeValue());
   account.setBalance(nodes.item(i+1).getNodeValue());
   account.setBalance(nodes.item(i+2).getNodeValue());
   return account;
}

Upvotes: 1

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 298908

I can't use an enum with the loop variable 'i'.

No, but you can use:

for(SomeEnum item : SomeEnum.values()){
  // code here
}

Upvotes: 1

Daniel
Daniel

Reputation: 1527

You can increment i by COLS_PER_ROW instead of 1 and then write:

for (int i = 0; i < aNodes.getLength(); i += COLS_PER_ROW) {
    account = new Account();
    String account = aNodes.item(i).getNodeValue();
    account.setAccount(account);
    String balance = aNodes.item(i+1).getNodeValue();
    account.setBalance(balance);
    String date = aNodes.item(i+2).getNodeValue();
    account.setDate(date);
    mAccounts.add(account);
}

Upvotes: 1

Related Questions