Hello there! I've created a method that searches for the customer with the highest deposit. Here's what I have so far:

  public Customer searchHighestDeposit(ArrayList<Customer> customers){


            for(int i=0; i<customers.size();i++)
                if(customers.get(i).getBankAccount().getBalance()>customers.get(i+1).getBankAccount().getBalance())
                    return customers.get(i);

                else
                    return customers.get(i+1);

            return null; 

The output is kinda weird: "The customer with the highest deposit is: bank.Customer@c390508"

The answer is supposed to be Richard.
I'm calling the method from main:

 System.out.println("The customer with the highest deposit is: "+bank.searchHighestDeposit(customers));

Just for the record:
bank.Customer@c390508 is the default toString() output (class name and hash) that every class inherits from Object.
In your own classes you should override toString() to return something useful - eg in this case the customer's name.

Anyway - the code still won't work - it wil just compare the first two customers and return the higher of those.

How do I search for the highest? I've tried using Collections.sort to find the highest but I'm getting an error.

Is it possible to have just one return statement?

Collections.sort will do the trick - you need to define a Comparator that compares the bank balances - but that's overkill for just getting the largest.

Hint:
Remember the code for finding the largest integer in an array?

int largestSoFar = array[0];
for (int i = 1; i < array.lemgth; i++) {
   if (array[i] > largestSoFar) largestSoFar = array[i]
}
return largestSoFar;

You can use exactly the same template to return the "largest" customer

I've tried the code but I'm getting another error:

            Customer highest;
            for(int i=1;i<customers.size();i++){
               if(customers.get(i).getBankAccount().deposit(i)>highest.getBankAccount().deposit(i))
                   highest = customers.get(i);       
            }
            return highest;
        }

Please never just say "get an error". Always post the complete text of all your error messgaes, along with the actual lines of code they refer to.

Anyway, in tis case it's obvious. You forgot to initialise highest, leading to a null pointer exception (unless your compiler spotted it first)

commented: It's obvious for you* =]] +6
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.