0

So I'm currently making a code right now that searches through arraylists and prints out the largest one. This code prompts the user for BankAccount names and how much money the account has. Currently I've run into the problem of not knowing how to search through arraylists for the biggest number. I also don't know how to compare each balance to the maxBalance. Any help is appreciated, Thanks.

        import java.io.*;
        import java.util.*;
        import java.text.*; 

        public class Project43Tester
        {
            public static void main(String args[])
            {
                NumberFormat formatter = NumberFormat.getNumberInstance( );
                formatter.setMinimumFractionDigits(2);
                formatter.setMaximumFractionDigits(2);
                String name;
                ArrayList <String> aryLst = new ArrayList<String>();

                do
                {
                    Scanner kbReader = new Scanner(System.in);
                    System.out.print("Please enter the name to whom the account belongs.(\"Exit\" to abort)");
                    name = kbReader.nextLine( );
                    if( !name.equalsIgnoreCase("EXIT") )
                    {
                        System.out.print("Please enter the amount of the deposit. ");
                        double amount = kbReader.nextDouble();
                        System.out.println(" ");
                        ArrayList <String> BankAccount = new <String> ArrayList();
                        AryLst.add(BankAccount);
                    }
                }while(!name.equalsIgnoreCase("EXIT"));

        Search aryList and print out the name and amount of the largest bank account

                BankAccount ba = //get first account in the list
                    double maxBalance = ba.balance;
                String maxName = ba.name;

                for(int j = 1; j < aryLst.size( ); j++)
                {
                    ?
                    ? Step through the remaining objects and decide which one has
                    largest balance (compare each balance to maxBalance)
                    ?
                }

                System.out.println(" ");
                System.out.println("The account with the largest balance belongs to " + maxName + ".");
                System.out.println("The amount that the account contains is $" + formatter.format(maxBalance) + ".");
            }
        }
user3201634
  • 41
  • 1
  • 1
  • 6
  • 2
    collections.sort() will sort your arraylist in Natural order. Then you can take the last element and convert it into a double... It is the largest element... – TheLostMind Feb 18 '14 at 08:11
  • 1
    This question is already asked http://stackoverflow.com/questions/8304767/how-to-get-maximum-value-from-the-list-arraylist and having answer too. – Naveed Yousaf Feb 18 '14 at 08:12

4 Answers4

1

You will have to traverse the ArrayList. During the traversal of the ArrayList, set the first element as max and compare it with next. If it is greater, then set the new element as the max.

Michael Aaron Safyan
  • 93,612
  • 16
  • 138
  • 200
Rahul
  • 3,479
  • 3
  • 16
  • 28
  • Its like re - inventing the wheel ... :) – TheLostMind Feb 18 '14 at 08:16
  • Not really, traversing the array to find the largest is kinda the effective way to do it. Involving sorting works too but can take a long time when the array grows... – Jakob Feb 18 '14 at 08:33
1

The easiest way is to use ArrayList's sort function. No headache there. Refer this link

Aditya Peshave
  • 667
  • 9
  • 26
0
List<String> l = new ArrayList<String>();
l.add("2.33");
l.add("3.45");
l.add("1.11");

Collections.sort(l);
for(String s: l) {
    System.out.println(s);
}

Result:

1.11
2.33
3.45
Enrichman
  • 11,157
  • 11
  • 67
  • 101
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
0

This is a slightly modified version of your code. Would advise to use a Comparable/Comparator for sorting (which you should try; modify below code).

Here's simple traversal through the array iterating and accessing object properties for your example.

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class Project43Tester
{
    static class BankAccount
    {
        String name;
        Double amount;

        public BankAccount(String name, Double amount)
        {
            this.name = name;
            this.amount = amount;
        }

        public String getName()
        {
            return name;
        }

        public void setName(String name)
        {
            this.name = name;
        }

        public Double getAmount()
        {
            return amount;
        }

        public void setAmount(Double amount)
        {
            this.amount = amount;
        }

    }

    public static void main(String args[])
    {
        NumberFormat formatter = NumberFormat.getNumberInstance();
        formatter.setMinimumFractionDigits(2);
        formatter.setMaximumFractionDigits(2);
        String name = null;
        List<BankAccount> bankAccounts = new ArrayList<BankAccount>();

        do
        {
            Scanner kbReader = new Scanner(System.in);
            System.out.print("Please enter the name to whom the account belongs.(\"Exit\" to abort)");
            name = kbReader.nextLine();
            if (!"EXIT".equalsIgnoreCase(name))
            {
                System.out.print("Please enter the amount of the deposit. ");
                double amount = kbReader.nextDouble();
                System.out.println(" ");

                bankAccounts.add(new BankAccount(name, amount));
            }
        }
        while (!"EXIT".equalsIgnoreCase(name));

        BankAccount maxBankAccount = null;

        for (BankAccount bankAccount : bankAccounts)
        {
            if (maxBankAccount == null)
            {
                maxBankAccount = bankAccount;
            }
            else if(bankAccount.getAmount() > maxBankAccount.getAmount())
            {
                maxBankAccount = bankAccount;
            }
        }

        System.out.println(" ");
        System.out.println("The account with the largest balance belongs to " + maxBankAccount.getName() + ".");
        System.out.println("The amount that the account contains is $" + formatter.format(maxBankAccount.getAmount()) + ".");
    }
}
aNish
  • 1,079
  • 6
  • 11
  • Hi! Thanks for the help but I have on question. I know for (BankAccount bankAccount : bankAccounts) is what BluePelican refers to as an advanced for loop but I didn't really understand it. can you convert that to a regular for loop? Thanks. – user3201634 Feb 18 '14 at 09:12
  • 1
    for (BankAccount bankAccount : bankAccounts) is equal to for(int j=0; j – aNish Feb 18 '14 at 09:28