Reputation: 23
I have created two arrays. One to store names and one to store sales. I am trying to link the indexes of each together so that index 1 from then name array will bring up the value of index 1 of the sales array. I am trying to get it to sort out the sales array and return the max sales and the person that brought in those sales. All the values are inputted by the user including the size of the arrays.
import java.util.Scanner;
import java.util.Arrays;
public class EmpArray {
public static int employee(){
Scanner input = new Scanner(System.in);
int numemp;
System.out.println("Enter how many employees to be compared: \n");
numemp = input.nextInt();
String name[] = new String[numemp];
int annsales[] = new int[numemp];
int maxannsales;
for (int i = 0; i < name.length; i++) {
System.out.println("Enter your name: \n");
String employeename = input.next();
name[i] = employeename;
System.out.println("\n");
}
for (int j = 0; j < annsales.length; j++) {
System.out.println("Enter in your annual sales: \n");
int employeesales = input.nextInt();
annsales[j] = employeesales;
System.out.println("\n");
}
System.out.println("Employee Name: " + Arrays.toString(name));
System.out.println("Total Sales: " + Arrays.toString(annsales));
Arrays.sort(annsales);
//look at page 456 of starting out with java and page 460 of the same book, p.464
System.out.println("Top Salary is : $"+annsales[annsales.length-1]);
maxannsales = annsales[annsales.length-1];
return maxannsales;
}
}
What am I doing wrong I have been at this for two weeks now.
Upvotes: 0
Views: 2010
Reputation: 8744
You should make a class to store the date rather than the use two separate arrays.
public class Employee {
private int sales;
private String name;
public Employee(String name, int sales){
this.name = name;
this.sales = sales;
}
public String getName(){
return this.name;
}
public int getSales(){
return this.sales;
}
}
Now store the name and sales as local variables when you read in from your Scanner and pass them to the Employee constructor. You can then create an Employee array[]
or ArrayList<Employee>
and sort this array/arraylist on Employee.getSales()
Like so:
import java.util.Scanner;
import java.util.Arrays;
public class EmpArray {
public static void main(String[] args){
employee();
}
public static int employee(){
Scanner input = new Scanner(System.in);
int numEmp;
System.out.println("Enter how many employees to be compared: ");
numEmp = input.nextInt();
input.nextLine();
Employee[] employees = new Employee[numEmp];
for (int i = 0; i < numEmp; i++) {
System.out.print("Enter your name: ");
String employeeName = input.nextLine();
System.out.println();
System.out.print("Enter in your annual sales:");
int employeeSales = input.nextInt();
input.nextLine();
System.out.println();
//creates an Employee object based on the constructor defined in Employee
Employee employee = new Employee(employeeName, employeeSales);
employees[i] = employee;
}
//initialize maxSeller to first employee
Employee maxSeller = employees[0];
for(int i = 0; i < employees.length; i++){
//using the getters we created in Employee
System.out.println("Employee Name: " + employees[i].getName());
System.out.println("Total Sales: " + employees[i].getSales());
System.out.println();
//checks if employees[i] sold more than maxSeller
if(maxSeller.getSales() < employees[i].getSales()){
maxSeller = employees[i];
}
}
System.out.println();
System.out.println("Top Seller: " + maxSeller.getName());
System.out.println("Top Sales: " + maxSeller.getSales());
return maxSeller.getSales();
}
}
Upvotes: 1
Reputation: 2937
If you sort the array with the numbers, then that gets sorted. You can find the top or bottom sales amount, but you cannot get the name of the salesperson. The two arrays are not linked in any way, so you cannot expect sorting one to cause the sorting of the other.
You need to take an approach that is fundamentally different., using different data structures.
For instance, one approach would be to have a single Array, but not to make it of String(s) or int(s), but to store both the name and the sales together, as a pair. You could pout the pair into an object as shown by sunrize920, or you could put them both in a map.
Having paired them together into some type of object, you can then have an Array of such objects.
Then, you could sort that array. To do so, you will need to write a custom comparator.
Alternatively, keep your two arrays and don;t do any sorting. Just loop through on and find the largest number. Remember the position of the largest sales number. Then, look up the name in the other array, using the same position.
Upvotes: 1