0

My code compiles but it returns -1 regardless of what I input, correct or not

public int search(String accountNumber){
       int index = 0;
       boolean found = false;

       for (Account account : accounts )
            while(index < accounts.size() && !found)
                if (accounts.get(index).equals (accountNumber)) 
                    found = true;  

                else 
                    index++;

        if (found == true)
            return index;
        else
            return -1;

    }
}

Essentially I'm trying to return the account number from that index position but I'm really struggling to solve this

Edit: Also I'm not sure how to correct this in order to make it work

/** Should remove the Account with the account number specified as a parameter if it the Account exists. The appropriate boolean value should then be returned via a method result dialog box. */

public boolean removeAccount(String accountNumber)
{
    int index = 0;
    for(Account account : accounts)
    {
        if((account.getAccountNumber() == accountNumber) && index < accounts.size())
            accounts.remove(account);
        else
            index++;
    }

}
  • 1
    Why don't you simply `return index` instead of setting `found = true`? And by the way, those two loops one inside the other are completely redundant. You only need **one** loop!!! For the sake of returning an index, the `while` loop would make an easier choice. – barak manos Nov 11 '14 at 11:47
  • @dana the problem is in your if statement under while loop. – Zeeshan Nov 11 '14 at 11:49
  • The simplest answer is do not use a for each loop use a normal for loop. If you find it then just return i; after the for loop just put return -1; – Roberto Anić Banić Nov 11 '14 at 11:50
  • Something is fundamentally wrong here. `accounts.get(...)` returns an `Account` object, while `accountNumber` is of type `String`. Would you mind explaining what exactly are you trying to compare??? – barak manos Nov 11 '14 at 11:59
  • @Dana it's not a good idea to remove item inside for each loop. For this purpose you should use iterator. [http://stackoverflow.com/questions/1196586/calling-remove-in-foreach-loop-in-java] – Zeeshan Nov 11 '14 at 12:51

7 Answers7

1

did you override the equals method in Account class. otherwise it will not work. because you compare Accounts with String. also if (found) is enough rather than if (found == true)

subash
  • 3,116
  • 3
  • 18
  • 22
1

You have two major problems:

  1. You are using a loop inside a loop, which is completely redundant for your purpose here
  2. You are comparing an Account object to a String object, which is probably not what you want

Here are the possible solutions:

Option #1:

for (Account account : accounts)
    if (account.getNumber().equals(accountNumber))
        return accounts.indexOf(account);
return -1;

Option #2:

int size = accounts.size();
for (int index=0; index<size; index++)
    if (accounts.get(index).getNumber().equals(accountNumber))
        return index;
return -1;

Of course, you will need to implement method public String getNumber() in class Account.

barak manos
  • 29,648
  • 10
  • 62
  • 114
0
public int search(String accountNumber){
   int index = 0;

   for (Account account : accounts ){
       if(account.getAccountNumber().equals(accountNumber))
          return index;
        else
           index++;
    }
   return -1;
}

Try this right now i dont have java setup but this should work. NOTE: Write a getter in your Account class which return account number.

************************************Solution for you second problem*****************************

public boolean removeAccount(String accountNumber)
{
    for (Account account : accounts) {
        if(account.getAccountNumber().equals(accountNumber))
          return accounts.remove(account);
    }
    return false;
}
Zeeshan
  • 619
  • 4
  • 16
0

You need a getNum() method in your Account class. Unless Account extends String. This should work properly and simplify things.

public int search(String accountNumber){
for(int i=0;i<accounts.size();i++){
 if(accounts.get(i).getNum().equals(accountNumber))
  return i;
}
return -1;
}
Roberto Anić Banić
  • 1,411
  • 10
  • 21
0

Just try this code:

public int search(String accountNumber){
   int index = 0;

   while(index < accounts.size()-1 && !Arrays.asList(accounts).contains(accountNumber)))
   {
       index++;
   }

    if  (accounts.get(index).equals (accountNumber))
        return index;
    else
        return -1;
    }
}

You don't need to iterate over all the arraylist, if you find it you return its index.

Well then try the Edit of the while condition.

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
0

You can try this code. I haven't written any new code, but just refined yours.

public int search(String accountNumber){
   int index = 0;
   boolean found = false;

   for (Account account : accounts )
   {
        if(index < accounts.size() && !found)
            if (accounts.get(index).equals (accountNumber)) 
                {
                 found = true;
                 break;
                }


            else 
                index++;

 }   
 if (found == true)
        return index;
    else
        return -1;

}
}
0

This should work try this..

public int search(String accountNumber)
{
 int index = -1;

 for(int i=0;i<accounts.size();i++)
 {
   if(accounts.get(i).getAccountNumber().equals(accountNumber))
    {
    index=i;
    break;
    }
 }
 return index;   
}

I have initialized index with -1 you may use 0 also and setter getter should be there in Account for accountNumber variable to write as accounts.get(i).getAccountNumber()

mady
  • 119
  • 1
  • 3
  • 12