Ziv Kesten
Ziv Kesten

Reputation: 1242

using a getter to get an Object out of an ArrayList

I am working on a Bank system exercise for class and i have a get function to get an Account object out of an ArrayList using the account id, it looks like this:

"Account" is a class, "accounts" is the ArrayList, "id" is a one of the values of the Object Account

public Account getAccount(int id){


    for (Account account : accounts) {
        if(((Account)account).getId()==id){
            return  ((Account)account); 

        }
    } 

}

however the function claims that there is no return statement (i work on Eclipse and that is the error it shows me) logcally it seems fine to me so i must have a syntax error of some sort, can anyone please spot my mistake?

thank you!

Upvotes: 1

Views: 293

Answers (6)

KhAn SaAb
KhAn SaAb

Reputation: 5366

Try this

 Account temp=null;

    if(((Account)account).getId()==id){
    temp=(Account)account); 
    }
    return temp;

Upvotes: 0

Lakshmi
Lakshmi

Reputation: 2294

You have make sure you return null if no account matches the id passed.

public Account getAccount(int id){

        for (Account account : accounts) {
            if(account.getId()==id){
               return (account); 

            }
        } 
    return null;

    }

Upvotes: 0

OrhanC1
OrhanC1

Reputation: 1410

The only way your function will return something is if the account with that particular ID exists in the list. You need a return statement that deals with the case where the account with that particular ID doesn't exist. Having said this, multiple return statements are generally bad practice so I recommend something like this:

public Account getAccount(int id) throws AccountNotFoundException{

    Account acc = null;
    for (Account account : accounts) {
        if(((Account)account).getId()==id){
            acc = (Account) account;
        }
    } 
    if(acc == null) throw new AccountNotFoundException();
    return acc;

}

Upvotes: 0

nano_nano
nano_nano

Reputation: 12523

if you define a return value you have to deliver one:

public Account getAccount(int id){
    Account found = null;
    for (Account account : accounts) {
        if(((Account)account).getId()==id){
            found = ((Account)account); 
            break;
        }
    } 
    return found;
}

or

public Account getAccount(int id){
    for (Account account : accounts) {
        if(((Account)account).getId()==id){
            return ((Account)account); 
        }
    } 
    return null;
}

found can be null in my example but you will always return Account or null.

btw. You should use a generic list like that:

    List<Account> accounts;

the advantage is that there is no typecast necessary.

Upvotes: 4

peter.petrov
peter.petrov

Reputation: 39437

You need to have a return statement after the for loop.

Also, you don't need all these brackets in your code.

public Account getAccount(int id){
    for (Account account : accounts) {
        if (account.getId()==id){
            return account; 
        }
    } 
    return null;
}

Upvotes: 0

TheLostMind
TheLostMind

Reputation: 36304

Its expecting an Account object to be returned "independent of any conditions".. You return Account only if

if(((Account)account).getId()==id){
            return  ((Account)account); 

add return null; at the end of the function...

Upvotes: 0

Related Questions