Get max value in list Java

TEST YOUR SMARTS
Which of the following retains the information it's storing when the system power is turned off?
  • ROM
  • CPU
  • RAM
  • GPU
Submit »
88% of IT pros got this right.
Challenge
×
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;

}

}

  • NetBeans.org NetBeans IDE[19]
  • Oracle Corporation Java[11]
Best Answer
Tabasco
OP
Joseph2982
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 max] max = list.getSales[];
View this "Best Answer" in the replies below »

9 Replies

· · ·
Tabasco
OP
Joseph2982
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 max] max = list[i].Sales;
2
· · ·
Tabasco
OP
RyanAZ
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 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
· · ·
Tabasco
OP
Joseph2982
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 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 max] max = list[i].getSales[];

Does it like this any better?

1
· · ·
Tabasco
OP
RyanAZ
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 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

Chủ Đề