Milmo Milmo - 10 months ago 90
Java Question

How to find the max value of an unsorted array

I am really new to java and I signed up for an AP class which is being taught very badly, and so I have no idea of how to do this part of the assignment. It prampts you to add code that will do the following

Find and print the maximum sale. Print both the id of the salesperson with the max sale and the amount of the sale, e.g., “Salesperson 3 had the highest sale with $4500.” Note that you don’t need another loop for this; you can do it in the same loop where the values are read and the sum is computed.

the code we are given is the following

import java.util.Scanner;
public class Sales {

public static void main(String[] args) {
final int salesPeople = 5;
int[] sales = new int[salesPeople];
int sum;

Scanner scan = new Scanner(;

for (int i=0; i<sales.length; i++) {

System.out.print("Enter sales for salesperson " + i + ":");
sales[i] = scan.nextInt();

System.out.println("\nSalesperson Sales");
sum = 0;
for (int i=0; i<sales.length; i++) {
System.out.println(" " + " " + sales[i]);
sum += sales[i];


System.out.println("\nTotal sales: " + sum);
System.out.println("Average sale " + sum/salesPeople);


I really do not know what to do and would appreciate a nudge in the right direction as to how to find the max and the id of the person who produced the max.

Please help this is homework!

Answer Source

Because it's your homework, I won't give you the direct answer, but instead give you some hints. If you add two variables, say,

int heighestSale = -1; // assume the sales are not negative or at least one sale is not negative int heighestPerson = -1;

then you update the two variables based on the current sale value in one of the loops and print out them at the end.