CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2008
    Posts
    8

    Help with assigning Array Size

    What the heck is the problem?

    code:

    // Determine the total number of employees
    inputEmployees = JOptionPane.showInputDialog(null, "How many employees do you have?");
    EMPLOYEES = Double.parseDouble(inputEmployees);

    // Create an array to hold employee salaries.
    double[] salaries = new double[EMPLOYEES];



    The error I'm getting is Salaries.java:33: incompatible types
    found : java.lang.Double
    required: int

    Why does it say required int? I made it Double.

  2. #2
    Join Date
    Sep 2008
    Posts
    8

    Re: Help with assigning Array Size

    Thanks, I took a different route and have another question.

    I need to Display the Names and Salaries of all Employees that are higher than the Average. I have included the code which shows how to display the highest salary of them all but am having trouble figuring out how to display this for each employee.



    public class Salaries
    {
    public static void main(String[] args)

    {
    // Create a Scanner object to read input.
    Scanner keyboard = new Scanner(System.in);


    String name;
    String input;
    double salary;
    String inputEmployees;
    double number;
    int EMPLOYEES;

    // Determine the total number of employees
    System.out.print("How many employees do you have?");
    EMPLOYEES = keyboard.nextInt();

    // Create an array to hold employee names.
    String[] names = new String[EMPLOYEES];

    // Create an array to hold employee salaries.
    int[] salaries = new int[EMPLOYEES];

    // Get each employee's name.
    for (int index = 0; index < names.length; index++)
    {
    System.out.print("What is the employee name for employee number " + (index + 1) + ": ");
    names[index] = keyboard.next();
    }


    // Get each employee's salary.
    for (int index = 0; index < salaries.length; index++)
    {
    System.out.print("What is the employee salary for employee number " + (index + 1) + ": ");
    salaries[index] = keyboard.nextInt();
    }


    // Display the average
    double total = 0; // initialize accumulator
    double average; // Will hold the average
    for (int index = 0; index < salaries.length; index++)
    total += salaries[index];
    average = total / salaries.length;
    System.out.println("The average is :" + average);


    // Display the highest salary
    int highest = salaries[0];
    for (int index = 1; index < salaries.length; index++)
    {
    if (salaries[index] > highest)
    highest = salaries[index];
    }

  3. #3
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Help with assigning Array Size

    Quote Originally Posted by oatmeal1201
    What the heck is the problem? ...
    Why does it say required int? I made it Double.
    The compiler is complaining about you trying to assign a double (the number of employees input) to an int (EMPLOYEES). There's no reason to make the number of employees a double - can you have .35 of an employee?

    In the particular is contained the universal...
    J. Joyce
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Help with assigning Array Size

    Quote Originally Posted by oatmeal1201
    I need to Display the Names and Salaries of all Employees that are higher than the Average. I have included the code which shows how to display the highest salary of them all but am having trouble figuring out how to display this for each employee.
    Can't you store the average salary, then run through all the employee salaries printing out those that are larger?

    [In a programming language] Simple things should be simple and complex things should be possible...
    A. Kay
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured