Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

problems printing from array list in hashmap

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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());
}
}

}
}
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Sam Smythe
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

MailItem@16f144c


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


And then in printMessages():
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic