I have a class that reads a file and then loads its contents into another file. It is just a part of the whole program. Here as i read the contents , there are around 15 rows / lines of content .But the file just reads 7 lines. Why so.
There a many problems with your code, all of which probably contribute in some way to the bug you are seeing.
You declare your file reference as a shared static variable. That is not a good start. Global variables need to be managed very carefully and unfortunately, you haven't done that in the rest of the code.
You are still doing file I/O operations of loading and saving inside a loop. You have been advised by others to put those operations outside your while-loop and yet you are still doing them inside.
Also, you should really take time to format/align your code properly so that it's not as confusing to read. When your code is not indented and aligned properly, it's very difficult to follow the logic. This makes understanding the flow of execution difficult and that can lead to bugs and difficulty in finding bugs.
Also, why do you have all that code in the EmployeeDTO constructor? Code in a constructor should be limited to setting up the instance with an initial stable state. That code does work that's not really related to initialization of an instance of the class.
@Junilu
Hi, thanks so far.
I tried doing it without a constructor and i get an compile time error message - expecting a constructor. I have worked with codes in the constructor previously so am unaware of how to go without it.
ALso , the problem of not all the lines getting saved and printed still persists. Do you have a solution
If you have code that is using the constructor to create new instances, then of course you'll get a compiler error if you delete it. I wasn't questioning the existence of a constructor, I was saying that the code you had in there didn't really belong in a constructor.
Regarding your main problem, did you address my question about why you still have line 18 inside the while loop?
Also, look at line 11. It uses the shared file reference in the FileWriter constructor. Then on line 25, you use the same file reference for the FileOutputStream constructor. This means you have two separate handles to the same file and you're doing I/O from both. Just like in Ghostbusters, you shouldn't cross streams like that.
Why don't you show us the contents of the input file, "compositepatternfile"? Also, how are you executing this code? Show us what the output of line 37 is as well. Please copy/paste directly so there won't be any manual typing errors.
The keys in a Map, which is what a Properties object is, are unique. You use b[3] as the key. Since multiple employees have the same manager, only the last employee that you record under a particular manager's key will remain in the Properties map.
So, the ones that get "clobbered" are "BLAKE" (7698) and "CLARK" (7782) -- only "JONES" (7566) will be recorded under the key 7839. You can go figure out the rest of the employees that get clobbered by other employees.
Your company is top ranked consultancy firm has a requirement from client.
Project manager was detailed about requirement and he approached you and here it is :
Client wants a report of entire organisation hierarchy starting from CEO where Manager and all his/her reportees will be printed on console in a tab separated tree like structure .So Read all employee data from file.print organisation hierarchy on console.
How to correct this code, i try to find the keys / employees id and match it with designation id manually in every while loop. A while loop for president, another while loop for manager,another while loop for the other designations. However here i encountered a problem in the second while loop was NULL, a string so i tried to replace it with 0000. But still it is not working
If it were up to me, I would first create a tree structure based on the self-referencing table that you have (that's what your input data is, a self-referencing table, because the b[3] element is a foreign key to another row's b[0] element). Once you have the tree structure, it's just a matter of finding the correct algorithm to print the tree out in a vertical structure.
This and my previous response has all the key words you need to use in a search engine to find examples of how to solve your problem. Good luck.