Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: can't compare class fields

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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
    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);
    	}

    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;
    	}
     
    }

    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);
    	}
     
    }
    Attached Images Attached Images
    Last edited by r0x; November 26th, 2011 at 04:32 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: can't compare class fields

    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

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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()

    if (dp.getSeqNo() == dataPackets.get(j).getSeqNo())

Similar Threads

  1. Compare 2 XML iles
    By deep in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 9th, 2011, 10:17 AM
  2. Compare instance of a class to another
    By srs in forum Java Theory & Questions
    Replies: 5
    Last Post: December 2nd, 2010, 10:39 AM
  3. Compare method
    By Evelina in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 16th, 2010, 02:07 PM
  4. Cannot set class fields using textInput
    By javascrub in forum What's Wrong With My Code?
    Replies: 0
    Last Post: August 26th, 2010, 04:04 AM
  5. how to compare two set values
    By humdinger in forum Collections and Generics
    Replies: 1
    Last Post: March 13th, 2010, 11:46 AM