简体   繁体   English

while循环不向每个循环添加数据新创建的对象? 在java中

[英]while loop not adding data to each loops newly created object? in java

Hi everyone once again thanks for taking the time to look at my issue. 大家好,再次感谢您抽出宝贵时间来研究我的问题。

I am trying to create a program that keeps track of employees and the diffrent departments that they are working in. 我正在尝试创建一个程序来跟踪员工和他们正在工作的不同部门。

The program first reads from a text file all the initial data to get the program going. 程序首先从文本文件中读取所有初始数据以使程序运行。 The program then has a while loop within a while loop. 然后程序在while循环中有一个while循环。 The first loop will read the department details and then create the department 第一个循环将读取部门详细信息,然后创建部门

in then the next (inner) while loop it reads all the employees accosiated with this department,It then after reading the details of the employee creates the employee and adds it to the previously created department to say this is the department I work in. 然后在下一个(内部)while循环中,它读取与该部门相关的所有员工,然后在阅读员工的详细信息后创建员工并将其添加到先前创建的部门,以说这是我工作的部门。

after adding all the employees to the department, it then exits that inner loop and sends the department with the employees inside it to the mainDriver for storage. 将所有员工添加到部门后,它会退出该内部循环,并将部门内的员工发送到mainDriver进行存储。 It does this for the remaining departments again adding their associated employees and so on. 它为其余部门再次添加相关员工等执行此操作。

The problem Is: it seems to create each department okay and add it to the mainDriver, but all the employees are added to the first department and the remaining department are left empty. 问题是:它似乎创建了每个部门并将其添加到mainDriver,但是所有员工都被添加到第一个部门,其余部门都是空的。 Which is not the way it should work as their are several employees in each department. 这不是应该工作的方式,因为他们是每个部门的几名员工。
Why is it not moving on to the next department as the new Department is instantiated?? 为什么它不会转移到下一个部门,因为新的部门被实例化?

Could I please have some help to see where I may be going wrong. 我可以请一些帮助,看看我可能会出错。

this is the code that read in the data. 这是读入数据的代码。

 while  (index < numberOfDepartmentsToRead ) 
{
        String depName1    = inFile.nextLine();
        String location1     = inFile.nextLine();
        String numberOfEmps = inFile.nextLine();
        int    numberOfEmps1 = Integer.parseInt(numberOfEmps);
        Department newDepartment = new Department(depName1 , location1);

    while (i < numberOfEmps1 )
    {
        String fName     = inFile.nextLine();
        String lName     = inFile.nextLine();
        String gender    = inFile.nextLine();
        String address   = inFile.nextLine();
        String   payLevel  = inFile.nextLine(); 
        int dPayLevel = Integer.parseInt(payLevel);
        Employee employeesFromList = new Employee(randomIDno, fName, lName, gender, dPayLevel);
        newDepartment.setAddEmp(employeesFromList, randomIDno);
        i++;
    }

    i = 0;
    index++;
    MainDriver.setDepartmentToSystem(newDepartment);        
} 

the employee is passed to this method in the departments class 员工在部门类中传递给此方法

public static void setAddEmp(Employee theEmp, int idNumber)
{
    employeesInThisDepartment.add(theEmp);
    employeeMap.put(idNumber, theEmp);
}

the department is added to the mainDriver classes storage method which is this 部门被添加到mainDriver类的存储方法就是这个

public static void setDepartmentToSystem(Department theDepartment)
        {

            allDepartments.add(theDepartment);
        } 
public static void setAddEmp(Employee theEmp, int idNumber)

Why is it static ? 它为什么是静态的 Make it instance method. 使它成为实例方法。

Make employeesInThisDepartment instance variable instead of static . 使employeesInThisDepartment实例变量而不是静态

You might want to check your use of static . 您可能想检查您对static的使用。 It is difficult to know without seeing all your code but I wonder if setAddEmp should not be a static method. 这是很难知道没有看到所有的代码,但我不知道是否setAddEmp 应该是一个静态方法。

Your employeesInThisDepartment is a static variable, whereas you need one per- Department . 您的employeesInThisDepartment是一个static变量,而您需要一个Department

Each Department should have its own instance, with an employees property, to which the department's employees are added. 每个Department都应该有自己的实例,其中包含employees财产,该部门的员工将被添加到该实体中。 Similarly, the method to add an employee to the department should be an instance method, not static. 同样,将员工添加到部门的方法应该是实例方法,而不是静态方法。

Binyomin, i think your inner while controller makes wrong... Binyomin,我认为你内在的控制者犯了错误......

while (i < numberOfEmps1 ){ i++; }

I think this loop will traverse all the employees in the file. 我认为这个循环将遍历文件中的所有员工。 Then the next iteration of the inner loop will return EOF... 然后内循环的下一次迭代将返回EOF ...

Try posting your file structure.. 尝试发布您的文件结构..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM