So here's my problem: My output is not what it should be, and I'm having the hardest time figure out why.
First, I read in the following two files:
pacs.txt is a list of every PAC, while member2pacs.txt is the list of all the people and their various pac memberships. Now, before you ask, there are two pacs that lack any members, so keep that in mind when reading my code.
Here is my code so far, I haven't yet written the print method to format it the way he wants, but everything else is as it should be. I even included a few debug prints to make sure my Pacs HashSet and PacMembership HashMap were being allocated correctly.
Now, when you run the program, you'll notice that only one person is ever assigned, and the rest of the groups are left empty. I'm not sure why its doing this, I do believe I've setup my loops correctly. Any ideas?
this is not a good way to tackle any problem.
You really should break everything down into small, testable methods. For example, i would have one method that reads one of your files. I would test the heck out of it. Have it print every line of the file to the screen, so that you know you are reading the entire file.
Have a second method that reads the other file, and again, print each line to the screen.
Have your main method call them one at a time to be sure they work. Then have your main method call both to be sure both work together.
have a method that parses each line of members2pacs. have it print out what it finds for each line.
have a method that inserts a name and a PAC into your map, and have it print out as it does it.
The less code you write before you recompile, the easier it is to fix. Sure, you will end up compiling about 200 times, but it will save you time in the long run.
p.s. NAMBLA? Really?
I should've stated this, I've already tested everything. The error is the loop in my compareFile method. Specifically, I believe it to be a problem with the inner for loop / iterator while loop. I'm not exactly sure what the problem is, but I know its there. I tried changing the order of the loop, moving around the while and inner for statement, even rewriting it completely to work from the iterator, but nothing fixed my problem (moving the iterator to the front got me stuck in an infinite loop).
As for as I can tell, I need to rewrite this:
I'm just not sure how I'd rewrite it... everything else up to this point works as expected.
Also, for the NAMBLA thing... its a homework assignment, my teacher put that. He setup the files and names of people, so that's not on me.
EDIT: Threw in a bunch of comments to explain what it should be doing / how it should run.
I'm not exactly sure what the problem is, but I know its there. I tried changing the order of the loop, moving around the while and inner for statement, even rewriting it completely to work from the iterator, but nothing fixed my problem (moving the iterator to the front got me stuck in an infinite loop).
Blindly guessing at solutions isn't the way forward. You need to analyse the problem and found out what is wrong before attempting to correct it. To find out what is happening you need to add lots of print statements in the code to make sure what you think is happening is actually happening.
If anyone wants to know the result, here:
Now, I do have one more question. I wrote a similar program to this, and like the last program, he wants the final output to be in sorted alphabetical order. I know the HashMap doesn't sort in any order, so to print from the hashmap in my last assignment, I simply moved everything over to an ArrayList, combining the Key+value into one string per each i in the ArrayList, sorted it, and simply printed one at a time. Is there a better way to print from a HashMap in sorted order than this? Especially since I don't know what the max size will be of the HashMap at any given time?
That looks as if you had simply printed the Map, rather than iterating its entry set or similar. I suggest you look in the Java Tutorials where it tells you how to iterate a Map. You may have to write your own code to format the printout.