1 Attachment(s)
can't compare class fields
hi ! There two classes
Node.java
DataPacket.java
The problem is I can't compare the field of the passed object "dp" with the field of the stored dataPacket, data packets are stored in the ArrayList "dataPackets". At the end I add also the full code for the Node class.
the Node.java contains the following method
Code :
public void acceptDPacket(DataPacket dp){
boolean packetHere;
for (int j = 0; j < dataPackets.size(); j++) {
if (dp.getSeqNo() = dataPackets[j].getSeqNo()){
packetHere = true;
break;
}
}
if (packetHere = true){
} else dataPackets.add(dp);
}
Code :
public class DataPacket {
private Node dest;
private Node source;
private int seqNo;
private String content;
public DataPacket(String cont, Node dest, Node src) {
this.content = cont;
this.dest = dest;
this.source = src;
}
public int getSeqNo(){
return seqNo;
}
public String getContent(){
return content;
}
}
Code :
import java.util.ArrayList;
public class Node extends Thread{
// constants initialization
final int MAP_LENGTH = 1000;
final int MAP_WIDTH = 1000;
private int x = 0;
private int y = 0;
private int node_id;
ArrayList<DataPacket> dataPackets = new ArrayList<DataPacket>();
//node_id = thread's name
public Node(String node_id) {
super(node_id);
this.x = (int) (MAP_LENGTH * Math.random());
this.y = (int) (MAP_WIDTH * Math.random());
}
public void run(){
System.out.println("Inside of node"+getNodeId()+" thread!");
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getNodeId() {
return this.node_id;
}
public void setNodeId(int nid){
this.node_id = nid;
}
public void acceptDPacket(DataPacket dp){
boolean packetHere;
for (int j = 0; j < dataPackets.size(); j++) {
if (dp.getSeqNo() = dataPackets[j].getSeqNo()){
packetHere = true;
break;
}
}
if (packetHere = true){
} else dataPackets.add(dp);
}
}
Re: can't compare class fields
Quote:
I can't compare the field of the passed object "dp" with the field of the stored dataPacket,
If you get errors, please post the full text of the error message.
Make sure your testing for equality uses the correct operator: ==
I see what looks like an assignment operator(=) in your code
Re: can't compare class fields
thanks for the reply!! the problem was also in accessing elements of the ArrayList, I forgot that there is a method get() :)
Code :
if (dp.getSeqNo() == dataPackets.get(j).getSeqNo())