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 2 of 2

Thread: Equals Method

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Equals Method

    Hey, I am having a problem getting a test to run correctly. Basically my equals method doesnt work when it is called if the name and the code variable are equal. I.E. the if statement doesnt work correctly.
    Here is the code...
    public class Denomination {
     
    	private String name;
    	private String code;
     
    	public static void main(String[] args)
    	{
     
    	}
    	public Denomination(String n, String c)
    	{
    		name = c;
    		code = n;
    	}
     
    	public String getName()
    	{
    		return name;		
    	}
    	public String getCode()
    	{
    		return code;
    	}
     
    	public boolean equality(Denomination a, Denomination b)
    	{
    		boolean equals = false;
    		if(a.getName().equals(b.getName()) && a.getCode().equals(b.getCode()))
    		{
    			equals = true;
    		}
    		return equals;
    	}
    }
    Last edited by helloworld922; February 18th, 2010 at 03:01 AM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Equals Method

    That method seems to work correctly. Are you sure that the Strings you are passing to the two different objects a and b are exactly the same? Remember Java is case sensitive i.e. "hello" != "Hello".

    You can compare two strings and ignore the case by using the equalsIgnoreCase() method.
    Last edited by Json; February 19th, 2010 at 04:18 AM. Reason: equalsIgnoreCase I think you mean

Similar Threads

  1. Arrays.equals not returning correct answer.
    By Anyone in forum Collections and Generics
    Replies: 2
    Last Post: February 11th, 2010, 10:12 AM