Results 1 to 9 of 9
  1. #1
    ookie833 is offline Member
    Join Date
    Nov 2008
    Posts
    9
    Rep Power
    0

    Default 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. #2
    timkd127 is offline Member
    Join Date
    Dec 2008
    Posts
    41
    Rep Power
    0

    Default

    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. #3
    ookie833 is offline Member
    Join Date
    Nov 2008
    Posts
    9
    Rep Power
    0

    Default

    I understand how to get the highest salery but how can I link that to the employees name?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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. #5
    Sidmyre is offline Member
    Join Date
    Nov 2008
    Posts
    7
    Rep Power
    0

    Default

    i wouldn't mind knowing the answer to this as well

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by Fubarable View Post
    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. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by Sidmyre View Post
    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. #8
    ookie833 is offline Member
    Join Date
    Nov 2008
    Posts
    9
    Rep Power
    0

    Default

    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. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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.

Similar Threads

  1. Need help with 2D arrays...
    By rrsv2 in forum New To Java
    Replies: 3
    Last Post: 11-30-2008, 03:15 AM
  2. Need help with Arrays
    By dietgal in forum New To Java
    Replies: 21
    Last Post: 10-08-2008, 01:59 PM
  3. new to arrays
    By jimJohnson in forum New To Java
    Replies: 1
    Last Post: 04-08-2008, 02:45 PM
  4. 2D-Arrays
    By kbyrne in forum New To Java
    Replies: 1
    Last Post: 02-07-2008, 10:08 PM
  5. arrays help
    By Warren in forum New To Java
    Replies: 6
    Last Post: 11-23-2007, 07:23 PM

Posting Permissions

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