| Author |
problems printing from array list in hashmap
|
Sam Smythe
Greenhorn
Joined: Jun 20, 2006
Posts: 12
|
|
The original code was supplied and we modified it as part of an assignment to add a hashmap and a print method, etc. I seem to have most of it working, but am having problems with the printMessages method. If there are no messages, I get a null pointer exception despite the if statement that should solve that. If there are messages, It works fine, except the message comes out as gibberish even though I called toString on it. Can anyone see the problems in the code? import java.util.ArrayList; import java.util.Iterator; import java.util.HashMap; /** * */ public class MailServer { //HashMap using generics private HashMap <String, ArrayList><MailItem>> messages; /** * construct MailServer and initialize HashMap */ public MailServer() { messages = new HashMap<String, ArrayList><MailItem>>(); } /** * return messages by user name * @param name is user request is made for * if no messages, return null */ public MailItem getNextMailItem(String name) { ArrayList<MailItem> lists = messages.get(name); if (lists == null) { return null; } Iterator<MailItem> it = lists.iterator(); while(it.hasNext()) { MailItem mess = it.next(); it.remove(); return mess; } return null; } /** * filter spam if message subject starts with "spam" or has "viagra" in subject * */ public void post(MailItem item) { String name = item.getTo(); //filter spam if (item.getSubject().toLowerCase().startsWith("spam") || item.getMessage().toLowerCase().contains("viagra")) { return; } if (! messages.containsKey(name)) { messages.put(name, new ArrayList<MailItem>()); } messages.get(name).add (item); } public void printMessages(String name) { ArrayList<MailItem> lists = messages.get(name); if (lists == null) { System.out.println("No messages"); } else { Iterator<MailItem> it = lists.iterator(); while(it.hasNext()) { MailItem mess = (MailItem)it.next(); System.out.println("Messages for: " + name); System.out.println(mess.toString()); } } } }
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14672
|
|
Works for me. (except that I had to correct <String, ArrayList><MailItem>> When no messages are in the map, could you please send the exception you get ?
|
[My Blog]
All roads lead to JavaRanch
|
 |
Sam Smythe
Greenhorn
Joined: Jun 20, 2006
Posts: 12
|
|
The no message works for me now too. We use BlueJ and sometimes it can be weird. I am still getting the gibberish if there is a message Messages for: two MailItem@16f144c The user's name was two...so that is fine, but I don't seem to be getting the mailitem back from object to string. I tried to add what you had in your answer, but BlueJ won't compile with it. Maybe I am doing it wrong...where did you put it?
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14672
|
|
MailItem@16f144c
This is not gibberish. You need to override the toString() method in MailItem. For example : And then in printMessages():
|
 |
 |
|
|
subject: problems printing from array list in hashmap
|
|
|