|
Hello all, I'm new in Java, I hope someone can help me out with my problem, since I have to do this a.s.a.p so I can go on with the GUI.
I want to have an array that recognized by any function in the class, I made following program already, but unfortunately it doesnt work
- on line 4 and 5 I defined array for role and agent
- then in function onMessage, I get following data:
- agentNumber
- agent0,agent1, agent2, etc (a)
- role0, role1, role2, etc (r)
the message i received is as following:
agent0="Dr. Shanti", role0 ="physician",
agent1="Mira", role1="patient", etc
until this line the program works,
I can see the result of line number 24
- then I would put the value of agent0, role0 to an array
agent, role with function inputAgent(a,r,i).
- I sent the value of a r and i to inputAgent(a, r, i);
- Until line 46 it works, I can see the result of
System.out.println( a+" "+r+" "+i+" "+ s);
but it FAILS to print
System.out.println( "input Agent " + agent);
value to agent
What I did wrong in the declaration of array?
I need to use this agent in almost every function, specially request function
---
1 package guiClient;
2
3 public class GuiClient implements MessageListener {
4 public static String[] agent ;
5 public static String[] role ;
6 private int agentNumber;
7 protected static ClientCommunication comm;
8
9
10 public GuiClient()
11 {
12 // create the AgentSystem connection
13 // set the client identifier for the RFID reader
14 }
15
16
17public void onMessage(ClientCommunication arg0, long arg1)
18 {
19 // retrieve pending message
20 IReceivedMessage message =
(IReceivedMessage)arg0.getMessage(arg1);
21 if (message.getPerformative().equals(PERF_REPLY+"-agents"))
22 { agentNumber=(message.getFromContent("count").hashCode());
23 if (agentNumber != 0 )
24 {System.out.println("no.of agents: "+ agentNumber);
25 for (int i=0; i<agentNumber; ++i)
26 { String a= (String)
27 message.getFromContent("agent"+i);
28 String r = (String)
29 message.getFromContent("role"+i);
30 System.out.println( a +" is a "+ r); 31 inputAgent(a,r,i);
32 }
33 }
34 }
35
36 // received a location message
37 // received a price message
38 // received a budget message
39 // receiveda schedule message
40 }
41
42
43 private void inputAgent(String a, String r, int i)
44 {
45 int s = agentNumber;
46 System.out.println( a+" "+r+" "+i+" "+ s);
47 agent = a;
48 System.out.println( "input Agent " + agent);
49 return;
50 }
51 public void requestagents()
52 { IMessageToSend msg = MFactory.instance().creStatement(
53 AgentElFactory.creAgentIdentifier("directoryAgent"),
54 SUBJ_GUI_CLIENT, PERF_REQUEST+"-agents",null);
55 comm.sendMessage((Message)msg);
56 }
57
58
59 public void requestlocation()
60 {
61 for (int i=0; i<agentNumber; ++i)
62 {
63 System.out.println( "request loc.for" + agent);
64 IMessageToSend msg = MFactory.instance().creStatement(
53 AgentElFactory.creAgentIdentifier(agent),
66 SUBJ_GUI_CLIENT, PERF_REQUEST+"-location",null);
67 comm.sendMessage((Message)msg);
68 }
69 }
70
71
72
73 public void requestprices()
74 { //same procedure as request location}
75
76
77 public void requestbudgets()
78 { //same procedure as request location}
79
80 public void requestschedule()
81 { //same procedure as request location}
82
83
84 public void closeConnection()
85 { comm.disconnect(); }
86
87
88
89 public static void main(String[] args) throws IOException
90 {
91 GuiClient guiClient = new GuiClient();
92 System.out.println("Main: Request Agents");
93 guiClient.requestagents();
94 System.out.println("Main: Request location");
95 guiClient.requestlocation();
96 System.out.println("Main: Request price");
97 guiClient.requestprices();
98 System.out.println("Main: Request budget");
99 guiClient.requestbudgets();
100 System.out.println("Main: Request schedule");
101 guiClient.requestschedule();
102 guiClient.closeConnection();
103 }
104
105 }
|
|
|
|
|
The declarations are okay. I do not see where you instantiated the arrays.
public void onMessage(ClientCommunication arg0, long arg1)
{
// retrieve pending message
IReceivedMessage message = (IReceivedMessage)arg0.getMessage(arg1);
if (message.getPerformative().equals(PERF_REPLY+"-agents"))
{
agentNumber=(message.getFromContent("count").hashCode());
if (agentNumber != 0 )
{
System.out.println("no.of agents: "+ agentNumber);
// now that you know the size that the arrays need to be
// you can instantiate them
agents = new String[agentNumber];
roles = new String[agentNumber];
for (int j=0; j<agentNumber; ++j)
{
String a = (String)message.getFromContent("agent"+j);
String r = (String)message.getFromContent("role"+j);
System.out.println( a +" is a "+ r);
inputAgent(a,r,j);
}
}
}
|
|
|
|
|
Thank you !!
maybe you can help me also with other problem I have.
I have 2 classes: Grundriss and GuiClient, in the Grundriss I have objects and in the GuiClient I have the process that will happen in the Grundriss.
In the GuiClient I received messages from other system:
location -> L
the value are: "waiting" "operation1" "operation2" etc.
sender -> S
the value are: "Dr. Frankenstein" "Schw. Maria" "Pat. Mueller" etc.
and in Grundriss I have object (label) with following name:
drFrankensteinWaiting
patMuellerOperation1
schwMariaOperation2
|
|
In following code I've succeed to get the name of my object in Grundriss, but my problem is, I can't call the string. I hope you understand my problem, to make clear, I should do:
grundriss.drFrankensteinWaiting.setVisible(true)
|
|
if I've got message from other system
if (message.getPerformative().equals(PERF_REPLY+"-location"))
if (message.getFromContent("location") != null )
{
String L=(String)message.getFromContent("location");
String S=(String)message.getSender().getName();
String loc=
L.replaceFirst(L.substring(0,1),L.substring(0,1).toUpperCase());
StringTokenizer t = new StringTokenizer(S, ". " );
String m = t.nextToken().toLowerCase();
String n = t.nextToken();
String agentlocation = m+n+loc;
// here I'm confused how i can get the value of the agentlocation
// as an object?
}
}
|
|
Thankyou again :)
|
|
|
|
|
|
|
|