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

Thread: error when comparing 2 string objects

  1. #1
    Member
    Join Date
    Nov 2010
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default error when comparing 2 string objects

    hello, in the following code i try to compare to string objects, however, i dont know where is my error so please help me

     
    public class Amr {
     
      private String word;
     
        public Amr(String str) 
        	{
        		setString(str);
            }
     
        private void setString (String str)
        {
        	this.word = str;
        }
     
        public boolean IsPloyndrome(Amr string)
            {
        	  if ( this.getString().equals(string.getString()) )
        	  	   return true;
        	  else 
        	  	   return false;
            }
     
        public  Amr getString()
            {
           	  Amr obj = new Amr(word);
           	  return obj;
            }
    }


    main method:

    public class Polyndrom
    {
    	public static void main (String[] args)
    	{
    		Amr h = new Amr("hi");
    		Amr b = new Amr("hi");
     
    		if ( h.IsPloyndrome(b) )
    			System.out.println ("equals");
    			else System.out.println ("not equals");
    	}
    }
    Last edited by amr; February 9th, 2011 at 07:36 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: error when comparing 2 string objects

    There is no need to double post your question - your other post has been removed. Next, each category has an announcements thread at the top and in it you will find explicit instructions on how to use the highlight/code tags. Next, you mention error: it helps to explain what error you are getting. You are lucky the code is somewhat straightforward that the problem is evident...look closely at how you are using the equals, you are not comparing String objects directly.

  3. #3
    Member
    Join Date
    Feb 2011
    Posts
    33
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: error when comparing 2 string objects


    Where's your main method?


    All intelligent thoughts have already been thought;
    what is necessary is only to try to think them again.



  4. #4
    Member
    Join Date
    Nov 2010
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: error when comparing 2 string objects

    would you please tell me how to use ( highlight) to organize the code being displayed

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: error when comparing 2 string objects

    Quote Originally Posted by amr View Post
    would you please tell me how to use ( highlight) to organize the code being displayed
    See Announcements

  6. #6
    Member
    Join Date
    Nov 2010
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: error when comparing 2 string objects

    thanx.................

Similar Threads

  1. Comparing Strings?
    By wandertheverse in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 4th, 2011, 10:32 PM
  2. String index out of bounds error 5???
    By stealthmonkey in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 26th, 2010, 07:11 PM
  3. Replies: 1
    Last Post: January 15th, 2010, 01:32 AM
  4. Replies: 2
    Last Post: November 3rd, 2009, 06:28 AM