I can't seem to Print out the Object I made in this Map. Any ideas?

Well here's my problem I'm trying to print out the Key's and Value's associated in the following MutliMap.

The code formating seems to screw up < and >. My datastructure is a Multi Map, the Key is a String Object and

the Value associated with that String Key is an ArrayList of an object of type Message I defined below.

Map<String,List><Message>> = new HashMap<String,List><Message>>();

Here's my code for where I try to Print everything (in the function print() ) .

The Print just either prints out the KEY and no value, if I take out my ((Message)value) cast, it will print the Message class, like

43343@Message because I'm assuming its wondering how to Print something that is an object I defined.

Thats why I defined my own print function and I also override the toString() function but both don't work it seems.

package parser;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.Iterator;

import java.util.List;

import java.util.Map;

import java.util.Set;

import java.io.*;

//this class should store all the Messages or "Events" and

//you can access them based on their Serial key.

publicclass MessageDB

{

//database to hold the information

//holds the Alerts/messages

Map<String, List><Message>> AlertMap;

//Constructor

MessageDB()

{

AlertMap =new HashMap<String, List><Message>>();

}

//print, outputs the contents of the hashMap

publicvoid print()

{

//want to print out the Key and all the Messages

//associated with that key

Set keys = AlertMap.keySet();// The set of keys in the map.

Iterator keyIter = keys.iterator();

System.out.println("The map contains the following associations:");

while (keyIter.hasNext()){

Object key = keyIter.next();// Get the next key.

Object value = AlertMap.get(key);// Get the value for that key.

System.out.println("Serial (key): " + key +"\n");

System.out.println("value: " + ((Message)value).toString() +"\n");

//((Message)value).print();

}

}

//overloaded print to print to user screen.

publicvoid print(PrintWriter out)

{

//want to print out the Key and all the Messages

//associated with that key

Set keys = AlertMap.keySet();// The set of keys in the map.

Iterator keyIter = keys.iterator();

out.println("The map contains the following associations:");

out.flush();

while (keyIter.hasNext()){

Object key = keyIter.next();// Get the next key.

Object value = AlertMap.get(key);// Get the value for that key.

//out.flush();

/* out.println( "(" + key + "," + value + ")" );

out.flush();*/

// out.println("\n");

out.println("EntityID: " + key +"\n"

+"Message: " + value +"\n");

//out.println("\n");

out.flush();

}

}

void add(Message msg)

{

//getting the position of the List by EntityID if avaiable

List<Message> AlertList = AlertMap.get(msg.Serial);

//checking to see if there is a unique Key already in the Map.

if (AlertList ==null)

{

//if there isnt a key in the map, add a new key, and a new List mapping

//to the key EntityID;

AlertList =new ArrayList<Message>();

AlertMap.put(msg.Serial, AlertList);

AlertList.add(msg);

}

else

{

//adding message to List

AlertList.add(msg);

}

}

}

Here's my message class:

package parser;

//this class will hold the Event/Message

import java.util.List;

import java.util.HashMap;

import java.util.Map;

import java.util.ArrayList;

import java.util.Set;

import java.util.Iterator;

publicclass Message{

String Identifer;

String Serial;

String Node;

String NodeAlias;

String Manager;

String Agent;

String AlertGroup;

String AlertKey;

String Severity;

String Summary;

String StateChange;

String FirstOccurance;

String LastOccurance;

String InternalLast;

String EventId;

String LocalNodeAlias;

Message()

{

Identifer ="";

Serial ="";

Node ="";

NodeAlias ="";

Manager ="";

Agent ="";

AlertGroup ="";

AlertKey ="";

Severity ="";

Summary ="";

StateChange ="";

FirstOccurance ="";

LastOccurance ="";

InternalLast ="";

EventId ="";

LocalNodeAlias ="";

}

void print()

{

System.out.println("Identifer: " + this.Identifer +'\n'

+"Serial: " + this.Serial +'\n'

+"Node: " + this.Node +'\n'

+"NodeAlias: " + this.NodeAlias +'\n'

+"Manager: " + this.Manager +'\n'

+"Agent: " + this.Agent +'\n'

+"AlertGroup: " + this.AlertGroup +'\n'

+"AlertKey: " + this.AlertKey +'\n'

+"Severity: " + this.Severity +'\n'

+"Summary: " + this.Summary +'\n'

+"StateChange: " + this.StateChange +'\n'

+"FirstOccurance: " + this.FirstOccurance +'\n'

+"LastOccurance: " + this.LastOccurance +'\n'

+"InternalLast: " + this.InternalLast +'\n'

+"EventId: " + this.EventId +'\n'

+"LocalNodeAlias: " + this.LocalNodeAlias +'\n');

}

public String toString()

{

return ("Identifer: " + this.Identifer +'\n'

+"Serial: " + this.Serial +'\n'

+"Node: " + this.Node +'\n'

+"NodeAlias: " + this.NodeAlias +'\n'

+"Manager: " + this.Manager +'\n'

+"Agent: " + this.Agent +'\n'

+"AlertGroup: " + this.AlertGroup +'\n'

+"AlertKey: " + this.AlertKey +'\n'

+"Severity: " + this.Severity +'\n'

+"Summary: " + this.Summary +'\n'

+"StateChange: " + this.StateChange +'\n'

+"FirstOccurance: " + this.FirstOccurance +'\n'

+"LastOccurance: " + this.LastOccurance +'\n'

+"InternalLast: " + this.InternalLast +'\n'

+"EventId: " + this.EventId +'\n'

+"LocalNodeAlias: " + this.LocalNodeAlias +'\n');

}

}

I'm correctly populating my Messages sent to me from the client, I tested it and it works but iterating over them once the messages are inserted into the map is my issue.

I'm not getting an error, just no output for the values

here's how I'm adding the Messages to the MessageDB

//This string is coming from a client

String str = in.readLine();

// creating a scanner to parse

Scanner scanner =new Scanner(str);

Scanner scannerPop =new Scanner(str);

//Creating a new message to hold information

Message msg =new Message();

//place Scanner object here:

MessageParser.printTokens(scanner);

MessageParser.populateMessage(scannerPop, msg);

System.out.println("-PRINTING MESSAGE\n");

msg.print();

System.out.println("-END PRINT-\n");

System.out.println("-Accessing data from Map-\n");

MessageDB testDB =new MessageDB();

testDB.add(msg);

testDB.print();

I know this code is ugly, I should make accessor and modifer functions for my Message class but for now I'm just trying to see if I this method is even going to work then I'm going to go about making it more protected.

Also here is my output:

that long mess is the incoming string from the client, and im parsing it up into tokens and assigning my message object certain values.

Address of server: 0.0.0.0/0.0.0.0

Server is bound to port: 2222

Echo :UPDATE:"GATEWAY:@barneyfifedisconnectedMon Jun 11 16:46:12

2007",446,"barneyfife","","ConnectionWatch","","Gateway","GATEWAY:",0,"A

GATEWAY process running on barneyfife has disconnected as username

gateway",06/11/07 16:50:12,06/11/07 16:46:12,06/11/07 16:46:12,06/11/07 16:46:12,0,1,1,0,0,"",65534,0,0,0,"",0,0,0,"","",0,0,"",0,"",0,0,"","","","","","","","",0,

0,"","","NCOMS",446,""

Token [0]UPDATE:"GATEWAY:@barneyfifedisconnectedMon Jun 11 16:46:12 2007"

Token [1]446

Token [2]"barneyfife"

Token [3]""

Token [4]"ConnectionWatch"

Token [5]""

Token [6]"Gateway"

Token [7]"GATEWAY:"

Token [8]0

Token [9]"A GATEWAY process running on barneyfife has disconnected as username gateway"

Token [10]06/11/07 16:50:12

Token [11]06/11/07 16:46:12

Token [12]06/11/07 16:46:12

Token [13]06/11/07 16:46:12

Token [14]0

Token [15]1

Token [16]1

Token [17]0

Token [18]0

Token [19]""

Token [20]65534

Token [21]0

Token [22]0

Token [23]0

Token [24]""

Token [25]0

Token [26]0

Token [27]0

Token [28]""

Token [29]""

Token [30]0

Token [31]0

Token [32]""

Token [33]0

Token [34]""

Token [35]0

Token [36]0

Token [37]""

Token [38]""

Token [39]""

Token [40]""

Token [41]""

Token [42]""

Token [43]""

Token [44]""

Token [45]0

Token [46]0

Token [47]""

Token [48]""

Token [49]"NCOMS"

Token [50]446

Token [51]""

Entering populate mssage insidecase 0

case 0: UPDATE:"GATEWAY:@barneyfifedisconnectedMon Jun 11 16:46:12 2007"

case 1: 446

case 2:"barneyfife"

case 3:""

case 4:"ConnectionWatch"

case 5:""

case 6:"Gateway"

case 7:"GATEWAY:"

case 8: 0

case 9:"A GATEWAY process running on barneyfife has disconnected as username gateway"

case 10: 06/11/07 16:50:12

case 11: 06/11/07 16:46:12

case 12: 06/11/07 16:46:12

case 13: 06/11/07 16:46:12

DEFAULT: 0

DEFAULT: 1

DEFAULT: 1

DEFAULT: 0

DEFAULT: 0

DEFAULT:""

DEFAULT: 65534

DEFAULT: 0

DEFAULT: 0

DEFAULT: 0

case 24:""

DEFAULT: 0

DEFAULT: 0

DEFAULT: 0

DEFAULT:""

DEFAULT:""

DEFAULT: 0

DEFAULT: 0

DEFAULT:""

DEFAULT: 0

DEFAULT:""

DEFAULT: 0

DEFAULT: 0

case 37:""

DEFAULT:""

DEFAULT:""

DEFAULT:""

DEFAULT:""

DEFAULT:""

DEFAULT:""

DEFAULT:""

DEFAULT: 0

DEFAULT: 0

DEFAULT:""

DEFAULT:""

DEFAULT:"NCOMS"

DEFAULT: 446

DEFAULT:""

-PRINTING MESSAGE

Identifer: UPDATE:"GATEWAY:@barneyfifedisconnectedMon Jun 11 16:46:12 2007"

Serial: 446

Node:"barneyfife"

NodeAlias:""

Manager:"ConnectionWatch"

Agent:""

AlertGroup:"Gateway"

AlertKey:"GATEWAY:"

Severity: 0

Summary:"A GATEWAY process running on barneyfife has disconnected as username gateway"

StateChange: 06/11/07 16:50:12

FirstOccurance: 06/11/07 16:46:12

LastOccurance: 06/11/07 16:46:12

InternalLast: 06/11/07 16:46:12

EventId:""

LocalNodeAlias:""

-END PRINT-

-Accessing data from Map-

The map contains the following associations:

Serial (key): 446

As you see it is printing the correct Serial, but it seems like it isn't even entering into the printing the value associated with that key.

NOTE: if you need more code I can supply my parsing functions as well.

Thanks!

Message was edited by:

lokie

NOTE: the code formating screws up the < >

it should be HashMap<String,List><Message>>();

Message was edited by:

lokie

Message was edited by:

lokie

Message was edited by:

lokie

[20043 byte] By [lokiea] at [2007-11-27 7:27:49]
# 1
Sorry, but I'm not going to read through all that. Could you post a short (< 1 page) example program for forum members to run that demonstrates your problem? Remember, in every big problem, there's a small one trying to escape.
Hippolytea at 2007-7-12 19:08:04 > top of Java-index,Java Essentials,Java Programming...
# 2

Thanks Good Idea :)

Heres a small version of the same problem:

MainTest

package ds;

public class MainTest {

public static void main(String []args)

{

//create a new message object, assign it some values.

//note: Serial is the key to the map.

Message m1 = new Message();

m1.Identifer = "Test Message";

m1.Serial = "222";

m1.Agent = "AgentTest";

m1.Identifer = "IdentiferTest";

//test to see if it assigned right.

m1.print();

//insert into a map

MessageDB dbTest = new MessageDB();

dbTest.add(m1);

//print key/values

dbTest.print();

}

}

Here is the 2 classes that I use, Message and MessageDB.

The problem is in the MesesageDB i'm getting the following error:

Exception in thread "main" java.lang.ClassCastException: java.util.ArrayList cannot be cast to ds.Message

at ds.MessageDB.print(MessageDB.java:43)

at ds.MainTest.main(MainTest.java:28)

package ds;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.Iterator;

import java.util.List;

import java.util.Map;

import java.util.Set;

import java.io.*;

//this class should store all the Messages or "Events" and

//you can access them based on their Serial key.

public class MessageDB

{

//database to hold the information

//holds the Alerts/messages

Map<String, List><Message>> AlertMap;

//Constructor

MessageDB()

{

AlertMap = new HashMap<String, List><Message>>();

}

//print, outputs the contents of the hashMap

public void print()

{

//want to print out the Key and all the Messages

//associated with that key

Set keys = AlertMap.keySet();// The set of keys in the map.

Iterator keyIter = keys.iterator();

System.out.println("The map contains the following associations:");

while (keyIter.hasNext()) {

Object key = keyIter.next(); // Get the next key.

Object value = AlertMap.get(key); // Get the value for that key.

System.out.println( "Serial (key): " + key + "\n");

System.out.println("value: " + ((Message)value) + "\n");

//((Message)value).print();

}

}

///adds a message (value) to the map, along with the key.

void add(Message msg)

{

//getting the position of the List by EntityID if avaiable

List<Message> AlertList = AlertMap.get(msg.Serial);

//checking to see if there is a unique Key already in the Map.

if (AlertList == null)

{

//if there isnt a key in the map, add a new key, and a new List mapping

//to the key EntityID;

AlertList = new ArrayList<Message>();

AlertMap.put(msg.Serial, AlertList);

AlertList.add(msg);

}

else

{

//adding message to List

AlertList.add(msg);

}

}

}

Here is the message class you'll need to use to compile:

package ds;

public class Message {

String Identifer;

String Serial;

String Node;

String NodeAlias;

String Manager;

String Agent;

Message()

{

Identifer = "";

Serial = "";

Node = "";

NodeAlias = "";

Manager = "";

Agent = "";

}

void print()

{

System.out.println("Identifer: " + this.Identifer + '\n'

+ "Serial: " + this.Serial + '\n'

+ "Node: " + this.Node + '\n'

+ "NodeAlias: " + this.NodeAlias + '\n'

+ "Manager: " + this.Manager + '\n'

+ "Agent: " + this.Agent + '\n');

}

public String toString()

{

return ("Identifer: " + this.Identifer + '\n'

+ "Serial: " + this.Serial + '\n'

+ "Node: " + this.Node + '\n'

+ "NodeAlias: " + this.NodeAlias + '\n'

+ "Manager: " + this.Manager + '\n'

+ "Agent: " + this.Agent + '\n');

}

}

It doesn't like this line as I suspected:

System.out.println("value: " + ((Message)value) + "\n");

lokiea at 2007-7-12 19:08:04 > top of Java-index,Java Essentials,Java Programming...
# 3

You declared alertMap to use List<Message> as it's value type,

Map<String, List><Message>> AlertMap;

but you try to cast it to a Message,

Object value = AlertMap.get(key); // Get the value for that key.

System.out.println("value: " + ((Message)value) + "\n");

That's your problem. Since you use generics for declaring the map, you should use them for the rest of the code as well. Using:

List<Message> value = AlertMap.get(key);

would make the error perfectly clear.

hunter9000a at 2007-7-12 19:08:04 > top of Java-index,Java Essentials,Java Programming...
# 4

It could be shorter:

public class MainTest {

public static void main(String[] args) {

Message m1 = new Message();

m1.serial = "222";

System.out.println(m1);

MessageDB dbTest = new MessageDB();

dbTest.add(m1);

dbTest.print();

}

}

//-

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

public class MessageDB {

Map<String, List >< Message > > alertMap = new HashMap <String, List >< Message > > ();;

public void print() {

System.out.println("The map contains the following associations:");

for (String key : alertMap.keySet())

System.out.println( "Serial (key): " + key + " values: " + alertMap.get(key));

}

void add(Message msg) {

List < Message > alertList = alertMap.get(msg.serial);

if (alertList == null)

alertMap.put(msg.serial, alertList = new ArrayList<Message>());

alertList.add(msg);

}

}

//--

public class Message {

String serial = "";

public String toString() {

return "serial: " + this.serial;

}

}

Hippolytea at 2007-7-12 19:08:04 > top of Java-index,Java Essentials,Java Programming...
# 5
Ahh excellent, thanks guys! That for each loop is wicked, I didn't even realize you could do it that way!5 stars for both of you :D
lokiea at 2007-7-12 19:08:04 > top of Java-index,Java Essentials,Java Programming...
# 6
You can even print out a map like this:System.out.println(theMap);
Hippolytea at 2007-7-12 19:08:04 > top of Java-index,Java Essentials,Java Programming...