Find max value in ArrayList Java

Get answers from your peers along with millions of IT pros who visit Spiceworks.
Join Now

I am writing a program for school and I can't figure out how to get the max value in a column. I need to find the highest Sales value in the ArrayList. I don't have a fixed number of Names so there could be 2 or 10 Salespersons Name,Sales in the arraylist depending on the quantity the user inputs. After the user finishes input I need to find the highest sales value so I can output each salesperson and display how much they need to meet or exceed the highest sales.

Here is how I get the data into the ArrayList:

ArrayList list = new ArrayList<>();

list.add(new Salesperson(myName,mySales));

The Salesperson class/constructor is setup like this:

private static class Salesperson{

private String name;

private double Sales;

public Salesperson(String n, double s){

this.Name = n;

this.Sales = s;

}

}

  • Find max value in ArrayList Java
    Oracle Corporation Java(11)
  • Find max value in ArrayList Java
    NetBeans.org NetBeans IDE(19)

Best Answer
Find max value in ArrayList Java
Tabasco
OP
Joseph2982
Find max value in ArrayList Java
This person is a verified professional.
Verify your account to enable IT peers to see that you are a professional.
Jan 20, 2015 at 19:55 UTC

::Smacks forehead::

It's an ArrayList, not an Array. Let's try angle brackets.

Java
double max = 0; for(int i = 0; i < list.size(); i++) if(list<i>.getSales() > max) max = list<i>.getSales();

9 Replies

· · ·
Find max value in ArrayList Java
Tabasco
OP
Joseph2982
Find max value in ArrayList Java
This person is a verified professional.
Verify your account to enable IT peers to see that you are a professional.
Jan 20, 2015 at 18:24 UTC

The length member variable should give you what you need. If not, try the size() member function.

Java
double max = 0; for(int i = 0; i < list.length; i++) if(list[i].Sales > max) max = list[i].Sales;
2
· · ·
Find max value in ArrayList Java
Tabasco
OP
RyanAZ
Find max value in ArrayList Java
This person is a verified professional.
Verify your account to enable IT peers to see that you are a professional.
Jan 20, 2015 at 18:39 UTC

Joseph2982 wrote:

The length member variable should give you what you need. If not, try the size() member function.

Java
double max = 0; for(int i = 0; i < list.length; i++) if(list[i].Sales > max) max = list[i].Sales;

Thanks for the help! I tried it and here is what I get:

I get errors. "array required, but ArrayList found.

Length wasn't an option so size() was an available option.

In the IF statement max= list[i].Sales; I get illiegal character: '\ufeff' and "array required, but ArrayList found"

1
· · ·
Find max value in ArrayList Java
Tabasco
OP
Joseph2982
Find max value in ArrayList Java
This person is a verified professional.
Verify your account to enable IT peers to see that you are a professional.
Jan 20, 2015 at 18:54 UTC

Ah! The Sales member is private. You will need to make it public or write a get() function. Best practice says, "make a get() function."

There is also a mismatch with capitalization in Name member.

Java
ArrayList <Salesperson> list = new ArrayList<>(); list.add(new Salesperson(myName,mySales)); // The Salesperson class/constructor is setup like this: private static class Salesperson { private String Name; private double Sales; public Salesperson(String n, double s) { this.Name = n; this.Sales = s; } public double getSales(){ return this.Sales; } } double max = 0; for(int i = 0; i < list.length; i++) if(list[i].getSales() > max) max = list[i].getSales();

Does it like this any better?

1
· · ·
Find max value in ArrayList Java
Tabasco
OP
RyanAZ
Find max value in ArrayList Java
This person is a verified professional.
Verify your account to enable IT peers to see that you are a professional.
Jan 20, 2015 at 19:03 UTC

Below is the code. I am still getting the same errors!

Java
package compcalc; import java.util.*; import java.text.DecimalFormat; /** */ public class CompCalc { private static class Salesperson { private String Name; private double Sales; public Salesperson(String n, double s) { this.Name = n; this.Sales = s; } public String getName(){ return this.Name; } public String getName(String newName){ return(this.Name = newName); } public double getSales(){ return this.Sales; } public double setSales(double newSales){ return (this.Sales = newSales); } } public static void main(String[] args) { //create variable to store sales value String myName; double mySales; int x=0; //initialize the ArrayList ArrayList<Salesperson> list = new ArrayList<>(); //set decimal format precision DecimalFormat fl = new DecimalFormat("#,###,##0.00"); //create new annualClass object annualCalc amt = new annualCalc(); //create scanner object for input Scanner keyboard = new Scanner(System.in); //set variables values amt.setGoal(120000); amt.setPayAt(80); amt.setSal(35000); amt.setRate(8); amt.setAcRate(1.25); do{ System.out.print("How many salespeople would you like to enter:"); //validate the input is a number while (!keyboard.hasNextInt()){ System.out.println("Invaid number. Please enter a number between 1 & 10:"); keyboard.next(); } x = keyboard.nextInt(); } while (x <= 0); //begin prompt loop for(int i=1; i<=x; i++){ //prompt for salesperson name System.out.print("\nEnter Salesperson's Name: "); myName = keyboard.next(); //prompt salesperson for annual sales amount System.out.print("\nEnter " + myName + " Annual Sales: $"); mySales = keyboard.nextDouble(); list.add(new Salesperson(myName,mySales)); } double max = 0; for(int i = 0; i < list.size(); i++){ if(list[i].getSales() > max) max = list[i].getSales(); { //display the results from arraylist and annualCalc class for (int index = 0; index < list.size(); index++) { Salesperson person = (Salesperson)list.get(index); amt.setAmt(person.getSales()); System.out.println("\nSalesperson :" + person.getName() + "\nSales: $" + fl.format(person.getSales()) + "\nAnnual Salary: $" + fl.format(amt.getSal()) + "\nCommission Earned: $" + fl.format(amt.getAmt()) + "\nTotal Compensation: $" + fl.format(amt.getTotal())); } System.out.println(); } }
1
· · ·
Find max value in ArrayList Java
Tabasco
OP
RyanAZ
Find max value in ArrayList Java
This person is a verified professional.
Verify your account to enable IT peers to see that you are a professional.
Jan 20, 2015 at 19:36 UTC

BTW.. The error is in the IF statement. The for loop is okay!

1
· · ·
Find max value in ArrayList Java
Tabasco
OP
Best Answer
Joseph2982
Find max value in ArrayList Java
This person is a verified professional.
Verify your account to enable IT peers to see that you are a professional.
Jan 20, 2015 at 19:55 UTC

::Smacks forehead::

It's an ArrayList, not an Array. Let's try angle brackets.

Java
double max = 0; for(int i = 0; i < list.size(); i++) if(list<i>.getSales() > max) max = list<i>.getSales();
1
· · ·
Find max value in ArrayList Java
Tabasco
OP
RyanAZ
Find max value in ArrayList Java
This person is a verified professional.
Verify your account to enable IT peers to see that you are a professional.
Jan 20, 2015 at 20:03 UTC

I haven't tried your suggestion yet, but I did find that the illegal character was caused by copy and paste. Seemed to have carried a line character over. I retyped it and it quit complaining.

1
· · ·
Find max value in ArrayList Java
Tabasco
OP
RyanAZ
Find max value in ArrayList Java
This person is a verified professional.
Verify your account to enable IT peers to see that you are a professional.
Jan 20, 2015 at 20:06 UTC
Java
double max = 0; for(int i = 0; i < list.size(); i++){ Salesperson s = (Salesperson)list.get(i); if(s.getSales() > max) max = s.getSales(); } System.out.println("The Max Value is " + max);

I was able to get the above code to work. Thank you so much for the guidance. I know to many languages already so I keep trying my VB, C, SQL, C++, C## operators and methods which is getting me in trouble.

1
· · ·
Find max value in ArrayList Java
Tabasco
OP
Joseph2982
Find max value in ArrayList Java
This person is a verified professional.
Verify your account to enable IT peers to see that you are a professional.
Jan 20, 2015 at 20:56 UTC

Interesting solution. It's curious to me that a disposable variable like that did the trick.

I am happy to help. Cheers!

0

This topic has been locked by an administrator and is no longer open for commenting.

To continue this discussion, please ask a new question.