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: Please Help me

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Please Help me

    Hello Coders i just registered and i love this site , i think i ll be here more often.


    i have a question that i couldn't solve i'm new at java here is the question ;

    Write a Java method called identical that takes 2 arrays of integers as arguments. The method should return true if the 2 arrays are identical; false otherwise. Hint: Two arrays are considered to be identical if they have the same length and the same values in the same order.
    b. Include your method in part a) into a Java program that initializes two arrays, invokes (calls) the method identical to check if the two arrays are identical or not and print a suitable message.


    And here is my code

     
    public class identicalMethod {
     
        public static void main(String[] args){
     
     
            int[] array  =  {1,2,3,4,5};
            int[] array2 =  {1,2,3,4,10};
     
           if (identical(array, array2))
               System.out.println("Its identical");
           else 
               System.out.println("its not");
     
     
        }
     
     
     
        public static boolean identical(int[] a , int[] b) 
        {
            boolean result = false ; 
            for (int i = 0; i <a.length; i++){
     
            if(a[i] == b[i]) 
            result = true;
     
     
     
     
        }
            return result;
     
    }
    }


    It always telling me that it's identical but it's not

  2. #2

    Default Re: Please Help me

    The identical() method is wrong!
    1. You should determine the length of a and b is equal or not;
    2. As you write, if a[i]==b[i], result is true.It means one value is same in a and b, the array of a and b is identical. You should determine all the numbers of a and b are equal or not.

        Code Removed

  3. The Following User Says Thank You to lym406365619 For This Useful Post:

    gambron (January 9th, 2014)

  4. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Please Help me

    @lym406365619: Your description of a useful identical() method was sufficient. Let the OP figure out how to code it but continue to be helpful if needed and if you can. Be helpful, but don't do the work.

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    gambron (January 9th, 2014)

  6. #4
    Junior Member
    Join Date
    Jan 2014
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Please Help me

    ohhh got it you mean to invert the if condition ,

    Thank you so much appreciate it

  7. #5
    Member Ganeprog's Avatar
    Join Date
    Jan 2014
    Location
    Chennai,India
    Posts
    80
    My Mood
    Love
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default Re: Please Help me

    HI,

    You just change the method like below, it will work fine.

    public static boolean identical(int[] a , int[] b)
    {
    boolean result = true ;
    for (int i = 0; i <a.length; i++){

    if(a[i] != b[i])
    result = false;





    }
    return result;

    }

    I changed only "if" condition then "flag" value tats it.

  8. The Following User Says Thank You to Ganeprog For This Useful Post:

    gambron (January 9th, 2014)

  9. #6
    Junior Member
    Join Date
    Jan 2014
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Please Help me

    Thank you i got the point , i love this forum. Since iam studying programming , i will visit a lot.