0

when I put any amount of numbers it give me wrong average I dont know why I tried many ways but still average doesnt work correctly what I did wrong to get wrong result? any suggestion..

import java.util.Scanner;
public class Assignment_Help2 {
    public static void main(String[] args){
        Scanner keyboard = new Scanner (System.in);

        String[] department = new String[5];
        int[] employeesno = new int[5];
        String [] address = new String[5];

        int index;
        index = 0;
        int totalentered = 0;
        String temp;

        // One loop to enter all information
        //INPUT LOOP
        for (index = 0; index < 5; index++)
        {
                System.out.print("Enter a department  or stop ..: ");
                department[index] = keyboard.nextLine();
                if (department[index].equals("stop"))
                {
                    break;

                }
                System.out.print("employees_no: ");
                temp = keyboard.nextLine();
                // convert string to a number & then store it as integer in the array
                employeesno[index] = Integer.valueOf(temp);

                System.out.print("Enter address: ");
                address[index] = keyboard.nextLine();   
                totalentered++;

        } //END OF FOR LOOP

        System.out.println("==================================");   

        System.out.println(String.format("%-20s", "department")+
                String.format("%-18s", "no_employees")+ 
                String.format("%-20s", "Address"));



        // Another separate loop to display the information stored in arrays
        //OUTPUT LOOP   
        for (index = 0; index < totalentered; index++)
        {

            System.out.println(String.format("%-20s", department[index]) + 
                     String.format("%-18d", employeesno[index])+ 
                     String.format("%-20s", address[index]));    

        } 

        int sum1=0;
        double average;
        for(int l=0; l <employeesno.length  ; l++) 
         {
           sum1 += employeesno[l];
         } average= sum1/employeesno.length;
         System.out.println("aver= "+average);
         keyboard.close();      
    }
}
3
  • 1
    Maybe you aren't there yet, but when it comes to storing an unknown quantity of elements you generally use a List instance rather than a low level array in Java. And you would use a class Department with the properties name, employees and address so you can use just one list. Commented Nov 9, 2019 at 14:30
  • I’m student and just started java it part of my assignment we did study list yet but I have tried create anew method and named it average still give me deferent results. Thanks for your reply Commented Nov 9, 2019 at 16:24
  • That's because although a list contains a size() method, you're keeping count differently. However, you forget about that variable in your loop to calculate the average (and the subsequent division). Commented Nov 9, 2019 at 16:34

1 Answer 1

1

sum1 is an int; employeesno.length is an int, so sum1 / employeesno.length is computed by integer division. Write

average = ((double) sum1) / employeesno.length`;

instead to carry out the division in doubles

Sign up to request clarification or add additional context in comments.

3 Comments

How large are the employee numbers? Maybe you're getting overflow trying to sum them up into an int. Try changing sum to a long.
I didnt put any employees limitation if that what you mean
Yeah. So if you put in 900000001, 900000002, 900000003, 900000004, and 900000005 for your employee numbers, summing those up would easily overflow the capacity of an int. Using a long for sum1 would solve that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.