Results 1 to 9 of 9
Thread: Help!! With arrays
- 12-11-2008, 02:31 AM #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));
}
}
- 12-11-2008, 03:51 AM #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];} } }
- 12-11-2008, 04:21 AM #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?
-
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, 10:57 AM #5
Member
- Join Date
- Nov 2008
- Posts
- 7
- Rep Power
- 0
i wouldn't mind knowing the answer to this as well
- 12-12-2008, 09:17 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 12-12-2008, 09:20 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 12-13-2008, 07:31 PM #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??
-
Similar Threads
-
Need help with 2D arrays...
By rrsv2 in forum New To JavaReplies: 3Last Post: 11-30-2008, 03:15 AM -
Need help with Arrays
By dietgal in forum New To JavaReplies: 21Last Post: 10-08-2008, 01:59 PM -
new to arrays
By jimJohnson in forum New To JavaReplies: 1Last Post: 04-08-2008, 02:45 PM -
2D-Arrays
By kbyrne in forum New To JavaReplies: 1Last Post: 02-07-2008, 10:08 PM -
arrays help
By Warren in forum New To JavaReplies: 6Last Post: 11-23-2007, 07:23 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks