# Help!! With arrays

• 12-11-2008, 03:31 AM
ookie833
Help!! With arrays
I do not understand how to do this.

Instructions: Use a loop to determine and display the name of the person with the highest salary (hint you will need to keep track of the index of the highest salary, then use this index to access the correct name).

MY PROBLEM:
The person with the highest salery is Chris Kennedy:\$61,500.00.
but I am unsure how to determine that with a loop?

Code I have:

import java.text.*;
import java.util.Scanner;
public class Exam2_1
{
public static void main (String [] args)
{
DecimalFormat currency = new DecimalFormat("\$#,##0.00");

double [] salery={ 45000.00, 27500.00, 61500.00, 32200.00, 51800.00};
String[] employees= { "John Smith", "Mary King", "Chris Kennedy", "Angela Jones", "Mark Smith"};

System.out.println(employees[3] + " has a salery of " + currency.format(salery[3]));

for (int index=0; index<employees.length; index++)
{
System.out.println(employees[index]);
}

double total=0;
double average;

for (int index=0; index < salery.length; index++)
{

total+=salery[index];

}
average=total/salery.length;
System.out.println("The average salery is " + currency.format(average));

double highest=salery[0];
for (int index=1; index < salery.length; index++)
{
if (salery[index] > highest)
highest=salery[index];
}
System.out.println("The highest salery is " + currency.format(highest));

}
}
• 12-11-2008, 04:51 AM
timkd127
you could put a field in such ass highestSalary or something and use a method like this to determine the highest.
Code:

```//field decleration double highestSalary=salery[0]; //method to determine the highest salary public void highestSalary(){ for(int i=1; i<salery.length;i++){           if(highestSalary<salery[i]){                     highestSalary=salery[i];}           } }```
• 12-11-2008, 05:21 AM
ookie833
I understand how to get the highest salery but how can I link that to the employees name?
• 12-11-2008, 05:29 AM
Fubarable
by using the index of the array, "i".

A better answer is not to use parallel arrays, but instead to create a class that holds both salary and name information, you could make the class implement the Comparable interface and then just sort a single array of objects of this class. But my guess is that your assignment doesn't allow this.
• 12-11-2008, 11:57 AM
Sidmyre
i wouldn't mind knowing the answer to this as well
• 12-12-2008, 10:17 AM
Eranga
Quote:

Originally Posted by Fubarable
by using the index of the array, "i".

A better answer is not to use parallel arrays, but instead to create a class that holds both salary and name information, you could make the class implement the Comparable interface and then just sort a single array of objects of this class. But my guess is that your assignment doesn't allow this.

I'm totally agreed with you lol. Working on with parallel arrays is more risky actually. There are lot of alternatives here.
• 12-12-2008, 10:20 AM
Eranga
Quote:

Originally Posted by Sidmyre
i wouldn't mind knowing the answer to this as well

Once you find the index of the highest salary, what you have to do is get the same index value on the other array. According to the OPs' code,

if the highest salary found at index 'i'
name of the person is employee[i]
• 12-13-2008, 08:31 PM
ookie833
I am having a problem...it says the types are incompatiable. String and double. I try this, but it says exception in main.

double highest=salery[0];
String highestPaid;
Double highestEmp;

for (list index=1; index < salery.length; index++)
{
if (salery[index] > highest)
highest=salery[index];
highestPaid=employees[index];
highestEmp=Double.parseDouble(highestPaid);
System.out.println("The employee with the highest pay is"
+ highestEmp);
}

I don't understand how to get the name to connect with index without a variable type error. Any ideas??
• 12-14-2008, 01:57 AM
Fubarable
You need to use curly braces to enclose the code in an if block that needs to be called if the if statement is true.