hello everyone,
Output is coming not equal for same input.what is the error in this program?

import java.util.*;
class newobj_equal
{
    public static void main(String[] args) 
    {
        int eid;
        String ename;
        double salary;

        Scanner sin=new Scanner(System.in);

        System.out.println("Enter eid");z
        eid=sin.nextInt();
        System.out.println("Enter ename");
        ename=sin.next();
        System.out.println("Enter salary");
        salary=sin.nextDouble();

        emp e1=new emp();
        e1.accept(eid,ename,salary);
        e1.display();

        System.out.println("Enter eid");
        eid=sin.nextInt();
        System.out.println("Enter ename");
        ename=sin.next();
        System.out.println("Enter salary");
        salary=sin.nextDouble();

        emp e2=new emp(); 
        e2.accept(eid,ename,salary);
        e2.display();

    if(e1.equals(e2))
    System.out.println("Both equal");
    else
    System.out.println("Both not equal");

    }
};

class emp
{
    int eid;
    String ename;
    double salary; 
    public void accept(int eid,String ename,double salary)
    {
        this.eid=eid;
        this.ename=ename;
        this.salary=salary;
    }
    public void display()
    {
        System.out.println("Employee details is:");
        System.out.println("\nEmp id="+eid+"\nEmp name="+ename+"\nEmp salary="+salary);
    }
    public boolean equals(Object obj)
    {
        if(obj instanceof emp)
        {
        emp o=(emp)obj;
        return(eid==o.eid && ename==o.ename && salary==o.salary);
    }
    else
    return false;
    }
};

You are comparing the two string name using == operator. You should use String.equals(); to compare two string. == operator will compare the two string object and will result true only when both the string objects are same.

import java.util.*;
class ClassOne
{
public static void main(String[] args) 
{
int eid;
String ename;
double salary;

Scanner sin=new Scanner(System.in);

System.out.println("Enter eid");
eid=sin.nextInt();
System.out.println("Enter ename");
ename=sin.next();
System.out.println("Enter salary");
salary=sin.nextDouble();

Employee e1=new Employee();
e1.accept(eid,ename,salary);
e1.display();

System.out.println("Enter eid");
eid=sin.nextInt();
System.out.println("Enter ename");
ename=sin.next();
System.out.println("Enter salary");
salary=sin.nextDouble();

Employee e2=new Employee(); 
e2.accept(eid,ename,salary);
e2.display();

if(e1.equals(e2))
System.out.println("Both equal");
else
System.out.println("Both not equal");

}
}

/////////////////////////////////////////////////////////////

class Employee
{
int eid;
String ename;
double salary; 
public void accept(int eid,String ename,double salary)
{
this.eid=eid;
this.ename=ename;
this.salary=salary;
}
public void display()
{
System.out.println("Employee details is:");
System.out.println("\nEmp id="+eid+"\nEmp name="+ename+"\nEmp salary="+salary);
}
public boolean equals(Employee obj)
{
/*if(obj instanceof Employee)
{
Employee o=(Employee)obj;
return(eid==o.eid && ename==o.ename && salary==o.salary);
}*/
if(this.eid==obj.eid && (this.ename).equals(obj.ename) && this.salary==obj.salary){
    return true;
}
else
return false;
}
}

Dude this shld work......thanks

ur oprogram will work if u change that ename==o.ename to ename.equals(o.ename)........bcoz we r comparing strings ,in emp class,equal method....
try this...

string.equals("string");

-->to compare two strings

string str==string str1--to compare objects

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.