aspose file tools
The moose likes Java in General and the fly likes problems printing from array list in hashmap Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "problems printing from array list in hashmap" Watch "problems printing from array list in hashmap" New topic
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
    
  11

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
    
  11

MailItem@16f144c

This is not gibberish.
You need to override the toString() method in MailItem.
For example :


And then in printMessages():
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problems printing from array list in hashmap
 
Similar Threads
help with HashMap
Why does getParameterMap() return an *immutable* Map?
Bad practise? JavaBean where setX() actually should be addX()?
HashMapping
Generics Question