I had encounter a problem in my task.
How come the output always return false?

public class Entity {
	private String itemCode;
	private String title;
	private String description;
	private int language;
	private int time;
	private String productCompany;
	private int status;

	public Entity(){
		itemCode="E250";
		title="The Lookout";
		description="by Scott Frank";
		language=1;
		time=1900;
		productCompany="BB Sdn. Bhd.";
		status=1;
	}

    public Entity(String code, String title, String description, int language, int time, String company, int status) {
    	itemCode=code;
		this.title=title;
		this.description=description;
		this.language=language;
		this.time=time;
		productCompany=company;
		this.status=status;

    }

    public void setItemCode(String code){
    	itemCode=code;
    }

    public String getItemCode(){
    	return itemCode;
    }

    public void setTitle(String title){
    	this.title=title;
    }

    public String getTitle(){
    	return title;
    }

    public void setDescription(String description){
    	this.description=description;

    }

    public String getDescription(){
    	return description;
    }

    public void setLanguage(int language){
    	this.language=language;

    }

    public int getLanguage(){
    	return language;
    }

    public void setTime(int time){
    	this.time=time;

    }

    public int getTime(){
    	return time;
    }

    public void setProductCompany(String company){
    	productCompany=company;

    }

    public String getProductCompany(){
    	return productCompany;
    }

    public void setStatus(int status){
    	this.status=status;
    }

    public int getStatus(){
    	return status;
    }

    public static void CheckObject(Entity[] object){
    	boolean test=false;
    	for(int i=0;i<object.length;i++){
    		if (object[i]!=null&&object[i].equals(object[i+1]))
    			 test=true;
    			 System.out.println(test);

    	}

    }

}
public class TestEntity {

    public static void main(String[] args) {
    	Entity film[]=new Entity[6];
    	film[0]=new Entity();
    	film[1]=new Entity("C305","Fu Lu Shou","jack",2,1500,"CC Sdn. Bhd.",2);
    	film[2]=new Entity("C305","Fu Lu Shou","jack",2,1500,"CC Sdn. Bhd.",2);
    	boolean check=false;

    	Entity.CheckObject(film);
    	System.out.print(check);

    	for (int i=0; i<film.length; i++){
    		if (film[i]!=null){

    		System.out.println(film[i].getItemCode()+ film[i].getTitle() + film[i].getDescription()+ film[i].getLanguage()
    			+ film[i].getTime() + film[i].getProductCompany() +	film[i].getStatus() );
    		}
    	}
    }


}

First of all in the CheckObject method you do this:

for(int i=0;i<object.length;i++){
     if (object[i]!=null&&object[i].equals(object[i+1]))

i+1 When you reach the last element of the array, meaning that i would be: object.length-1 you will call the object . But then you will also call the object[i+1] which will give you an exception because i+1 exceeds the arrays length.
Assuming that length is 4: 0,1,2,3 when i=3 you call: object[3].equals(object[4]) But object[4] is wrong because the length of the array is 4.
I would advice this:

for(int i=0;i<object.length-1;i++){
     if (object[i]!=null&&object[i].equals(object[i+1]))

So in my previous example, when i=2 you will do: object[2].equals(object[3]) Then when you reach i=3 (i<object.length-1)=(3<3) you will not enter the loop because you have reached the last element and there is no next to compare it with.

Also you need to remove the check variable from the TestEntity class. It doesn't do anything. You declare it false and then you print it. It is NOT the same with the one declared locally in the CheckObject method.

And the reason why the CheckObject doesn't return true is because the method equals returns false: (object.equals(object[i+1]))

film[1]=new Entity("C305","Fu Lu Shou","jack",2,1500,"CC Sdn. Bhd.",2);
film[2]=new Entity("C305","Fu Lu Shou","jack",2,1500,"CC Sdn. Bhd.",2);

Those objects are different, they are not the same. The equals returns false at those. All classes are subclasses of the class java.lang.Object. That class has a equals(Object) method. You haven't declared such method in the Entity class so it will call the method from the Object class:
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Object.html#equals%28java.lang.Object%29

That method is implementes so that it will return true only if they are the same, not if they have the same value:

Object a = [I]some value[/I]
Object b = a;
Object c = [I]same value[/I]

// b and a are the same
// c and a are not

That is why when you want to compare String you use the equals method. Because the class java.lang.String overrides the equals method. You should do the same. In the Entity class:

public boolean equals(Object obj) {
  if (obj==null) return false;
  try {
      Entity en = (Entity)obj;
      boolean b = false;

      if (itemCode==null) b = (en.getItemCode()==null);
      else b = itemCode.equals(en.getItemCode());
      if (b==false) return false;

      b = language==en.getLanguage();
      if (b==false) return false;

      // the same for the rest

     return b;
  } catch (Exception e) {
    return false;
  }
}

The try-catch is there because the equals should never throw an exception. If you haven't learned about exceptions remove that code, in case you get into trouble with your teacher.

Thank you for the reply...I know the way solve to solve the problem already.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.