1. Member
Join Date
Nov 2008
Posts
9
Rep Power
0

## 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));

}
}

2. Member
Join Date
Dec 2008
Posts
41
Rep Power
0
you could put a field in such ass highestSalary or something and use a method like this to determine the highest.
Java 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];}
}
}```

3. Member
Join Date
Nov 2008
Posts
9
Rep Power
0
I understand how to get the highest salery but how can I link that to the employees name?

4. 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.

5. Member
Join Date
Nov 2008
Posts
7
Rep Power
0
i wouldn't mind knowing the answer to this as well

6. 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.

7. 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]

8. Member
Join Date
Nov 2008
Posts
9
Rep Power
0
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??

9. 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.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•