• Post Reply Bookmark Topic Watch Topic
  • New Topic

why is the output coming halfway in Properties file  RSS feed

 
Sucheta Shrivastava
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sucheta Shrivastava
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Junilu
I have changed my code to this , still it is showing the same bug of printing only 7 lines instead of 12.

 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code looks much better now that it's properly indented and aligned.

Why is line 18 still inside the while loop?  The store() method needs to be called only once. You're already doing that on line 26.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sucheta Shrivastava
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sucheta Shrivastava
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my formatted code. But my main problem still exists of not all the lines getting written on the properties file. WILL IT EVER GET SORTED.


 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sucheta Shrivastava
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
compositepatternfile :




employee.properties




output of  console :

 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Sucheta Shrivastava
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh....  got it. so what is the solution
 
Sucheta Shrivastava
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question  goes like this

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.
 
Sucheta Shrivastava
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so i thought i shall entry that in the hashmap and with keys i shall find the president, manager and so on the hierarchy goes like that
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest you search for information on how to print out a tree structure and self-referencing tables hierarchy
 
Sucheta Shrivastava
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!