• Post Reply Bookmark Topic Watch Topic
  • New Topic

HashMap and HashSet - Unexpected Output / Loop help  RSS feed

 
Joseph Ibrahim
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

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

member2pacs.txt


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?


Thanks,
Joe
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is a LOT of code to have written only to find out now it doesn't work. And, it all seems to be in a single method.

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.

etc.

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?
 
Joseph Ibrahim
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply,

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.

Thanks,
Joe

EDIT: Threw in a bunch of comments to explain what it should be doing / how it should run.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well to add to everything Fred said I would certainly be trimming all those strings to make sure there are no spurious whitespace chars in there.

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.
 
Joseph Ibrahim
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I seemed to have solved it on my own. I was forgetting some cases, once I got those in there, cleaned up a little bit, I was easily able to finish everything.

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?

Thanks,
Joe
 
Paweł Baczyński
Bartender
Posts: 2074
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you may consider using TreeMap.
 
john kidwell
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey I have the same prompt as this and I'm looking at your code. I have it all coming out correctly though it's not matching the correct output. My output is



but I want my output to be




I feel like it's something small. But I'm not sure what it is.
 
Joseph Ibrahim
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're doing:
System.out.println(hashmap);

That'll printout like that. You need to write a print method to print it out the way its supposed to be organized.
Its a pretty simple method, I used ArrayList to do it... though I'm not sure if we're supposed to. But it works.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

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.
 
john kidwell
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried printing it differently but I now i got an output of



How did you build your arraylist to print it correctly?
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And how are you printing those memberships?
 
Joseph Ibrahim
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yo,

Here's the print method I used:


Like I said, I'm sure there are better and faster ways to do it, but that's all I could think of.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys, this is not a forum to share the answers to your homework questions, that can be perceived as cheating.
The forum is so you can ask questions and be given guidance on how you can write the code yourself.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!