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: Arrays.equals not returning correct answer.

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Arrays.equals not returning correct answer.

    Hi,

    I am having some problems with Arrays.equals. I can't get it to return true and I can't see the problem.

        public void testing()
        {
        boolean check = Arrays.equals(test, test2);
        if (check == false)
        {
            System.out.println("Word is not a palindrome!");
             for ( int i=0; i < test.length; i++){
                System.out.println(test[i] + "       " + test2[i]);
            }
        }
            else
            {
                System.out.println("Word is a palindrome!");
                        for ( int i=0; i < test.length; i++){
                System.out.println(test[i] + "       " + test2[i]);
            }
            System.out.println("Isn't this an exciting game!");
        }
     
    }

    test is a word input by the user, test2 is test copied to test2 and reversed. If I print the arrays they seem to be the same. If i change it to compare test to test or test2 to test2 it tells me they are the same so I guess I am just missing something. Can anyone point me in the right direction?

    Thanks
    Last edited by Anyone; February 6th, 2010 at 04:31 PM.


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

    Default Re: Arrays.equals not returning correct answer.

    I suspect the problem may be that you have capital and lower-case letters? Java is case sensitive.

  3. #3
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Arrays.equals not returning correct answer.

    Maybe the fact that you're reversing one of the arrays is making them not equals each other any more.

    // Json

Similar Threads

  1. 2d Arrays
    By mgutierrez19 in forum Collections and Generics
    Replies: 5
    Last Post: October 27th, 2009, 05:08 PM
  2. throwing and returning
    By chronoz13 in forum Exceptions
    Replies: 6
    Last Post: October 25th, 2009, 01:00 AM
  3. Returning Random Strings from an Array
    By cfmonster in forum Collections and Generics
    Replies: 3
    Last Post: September 9th, 2009, 12:13 AM
  4. Elegant and short way to implement my program on 2d Array
    By TheForumLord in forum Collections and Generics
    Replies: 1
    Last Post: December 8th, 2008, 04:56 PM
  5. Java error in using Arrays
    By AnithaBabu1 in forum Collections and Generics
    Replies: 4
    Last Post: November 4th, 2008, 07:50 AM