Results 1 to 14 of 14
  1. #1
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default logic flaw in comparison?

    Does this follow a typical argument structure, or is my for loop just not correct?




    public static boolean areEqual (int[] a1, int[] a2)
    {

    boolean equalArrays = true;

    for( int i = 0; i < a1.length; i++)
    {

    if (
    (

    (a1 == null && a2 == null)

    &

    ( a1.length == a2.length)

    &

    (a1[i] == a2[i])
    )
    )
    return equalArrays;
    else return false;
    }
    return boolean;
    }

    }

  2. #2
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by Adomini View Post
    Does this follow a typical argument structure, or is my for loop just not correct?
    Please clarify this statement as it doesn't make sense to me. Also, you'll want to edit your post above so that code tags are used above and below your posted code so that it will retain its formatting (the forum FAQ will show you how). If the code is easier to read, more will read it.

  3. #3
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default

    Right. I am trying to use a Boolean argument in comparing two int [] arrays. Using a Boolean argument, logically, is what i was trying to do. If two arrays are both null or they have the same length and have the same elements, then they are the same.

    Java Code:
    public static boolean areEqual (int[] a1, int[] a2)
    { 
    
    boolean equalArrays = true;
    
    for( int i = 0; i < a1.length; i++)
    {
    
    if ( 
    (
    
    (a1 == null && a2 == null)
    
    &
    
    ( a1.length == a2.length)
    
    &
    
    (a1[i] == a2[i])
    )
    )
    return equalArrays;
    else return false;
    }
    return boolean;
    }
    
    }

  4. #4
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default

    after the null comparison, I just switched an 'or' (|) sign instead of the 'and' (&) sign, to make more sense.

  5. #5
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    There's something wrong with your code indentation, and it's still quite hard to read.

    You want to ask yourself a few questions:
    1) Do you want to check if the arrays are null once before the for loop or do you want to do this inside the for loop with each iteration of the loop?
    2) Same for each array's length -- do you want to check it once before the for loop or do you want to check with each iteration of the for loop?

    You'll want to think through your code logically, stepping into it walking through it mentally just as if you were the computer.

  6. #6
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default

    Hi,

    First you should know the difference between the "&&" logic operator and the "&" bitwise operator.

    true && true = true
    true && false = false

    0000 & 0000 = 0000
    1010 & 1000 = 1000

  7. #7
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by wsaryada View Post
    Hi,

    First you should know the difference between the "&&" logic operator and the "&" bitwise operator.

    true && true = true
    true && false = false

    0000 & 0000 = 0000
    1010 & 1000 = 1000

    wsaryada: While & is a bitwise operator, you do know that & is also a boolean operator just like && the difference being && will short circuit if the first test is false while & won't. So you understand that this is perfectly valid, right?:

    Java Code:
    true & true = true
    true & false = false
    Point number 2: what the heck does this have to do with the original poster's question or problem?
    Last edited by curmudgeon; 08-31-2010 at 04:57 AM.

  8. #8
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default

    Thanks for the response guys. What I though I could do was have everything under the for loop and have it say
    1) if both are null, they are equal
    2) if they are the same length AND have the same elements, then they are equal.

    Otherwise, they are false.

    i will check the indention and see if that is what is causing the problem. I guess I only need the for loop for the length comparison, but doing that looks kinda funny to me.

    Only reason I had '&' and not "&&" is because a similar expression in the Java book did it, so it just happened to work too.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    As noted above, will the length change as you loop through the for loop? Or is the length of each array the same throughout? If it doesn't change, why check it inside the loop. I think that the only thing that should be checked in the loop is to see if the i'th item from one array is the same as the i'th item from the other array, that's it. And you're not going to want to even do the loop if either array is null or the lengths are different.

  10. #10
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default

    the length will change as we go through the loop. This is supposed to be a blank formula/ code that can take the length of any arrays and compare them.
    I am having a small issue with the return false statment at the end however.

    I am trying to get the computer to see that if none of those three true conditions meet, then the result should be false.

    Java Code:
    public static boolean areEqual (int[] a1, int[] a2)
    	{  // why is this giving me an error? That belongs there!
    	
    		boolean equalArrays = true;
    		
    		if ( (a1 == null && a2 == null))
    			return equalArrays;
    		
    		if ( a1.length == a2.length)
    			return equalArrays;
    
    		for( int i = 0; i < a1.length; i++ )
    		{
    			
    			if (a1[i] == a2[i])
    			    
                   return equalArrays;
    		}
    		
    		
    		else return false;
    		
    		
    		
    	}

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Adomini View Post
    the length will change as we go through the loop.
    How? The length of an array never changes once it is set. Yes, this method should take arrays of different lengths, and yes, each time the method is called the arrays passed into the parameters may have different lengths from before, but each time the method is called, within the method code itself the length of both arrays do not change throughout the method; again, they simply can't. Period. End of story. Finito.


    I am having a small issue with the return false statment at the end however.

    I am trying to get the computer to see that if none of those three true conditions meet, then the result should be false.

    Java Code:
    public static boolean areEqual (int[] a1, int[] a2)
    	{  // why is this giving me an error? That belongs there! [color="red"]what is the error?[/color]
    	
    		boolean equalArrays = true;
    		
    		if ( (a1 == null && a2 == null))
    			return equalArrays;
    		
    		if ( a1.length == a2.length)
    			return equalArrays; // [color="red"]are you sure about this line?[/color]
    
    		for( int i = 0; i < a1.length; i++ )
    		{
    			
    			if (a1[i] == a2[i])
    			    
                   return equalArrays; // [color="red"]are you sure about this line?[/color]
    		}
    		
    		
    		else return false;
    		
    		
    		
    	}
    See comments above.

    The key to solving this is to write down how you would solve this on paper. If you had two lists of numbers, how would you see if both are the same? What steps would you take? And when in the process do you say, I'm done, the answer is ***? This is the equivalent of the return statement above.


    For example, I'd do something like:
    First check if the lists exist. If one doesn't exist, I'd complain (that's called throwing an exception in Java) and exit.
    Next I'd check if the arrays were the same size. If they were different size, I'd say that the answer is that they're not equal and would exit (return).

    Next I'd loop through the arrays, comparing each item at the same level or index as I looped through them, if any item is different from the other, I'd give the answer that the arrays were different and would exit.

    If I'm now at the end and didn't exit, then I'd say the arrays were the same.

    Do you see how this logic is different from yours above?
    Luck.
    Last edited by Fubarable; 08-31-2010 at 06:09 AM.

  12. #12
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default

    Quote Originally Posted by curmudgeon View Post
    wsaryada: While & is a bitwise operator, you do know that & is also a boolean operator just like && the difference being && will short circuit if the first test is false while & won't. So you understand that this is perfectly valid, right?:

    Java Code:
    true & true = true
    true & false = false
    Point number 2: what the heck does this have to do with the original poster's question or problem?
    Yes, you are correct. My bad, I didn't look at the code deeper :D

  13. #13
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default

    Quote Originally Posted by Adomini View Post
    the length will change as we go through the loop. This is supposed to be a blank formula/ code that can take the length of any arrays and compare them.
    I am having a small issue with the return false statment at the end however.

    I am trying to get the computer to see that if none of those three true conditions meet, then the result should be false.

    Java Code:
    public static boolean areEqual (int[] a1, int[] a2)
    	{  // why is this giving me an error? That belongs there!
    	
    		boolean equalArrays = true;
    		
    		if ( (a1 == null && a2 == null))
    			return equalArrays;
    		
    		if ( a1.length == a2.length)
    			return equalArrays;
    
    		for( int i = 0; i < a1.length; i++ )
    		{
    			
    			if (a1[i] == a2[i])
    			    
                   return equalArrays;
    		}
    		
    		
    		else return false;
    		
    		
    		
    	}
    Your else-block doesn't match any corresponding if statement. Or just remove the else and says return false.
    Last edited by wsaryada; 08-31-2010 at 06:21 AM.

  14. #14
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default

    Thanks everyone. I think I got it now. Fubarable, that makes sense. It must be the late hour......... dont know why I missed it.

Similar Threads

  1. need a logic for this
    By rajivjoshi in forum New To Java
    Replies: 4
    Last Post: 06-12-2010, 02:18 PM
  2. Comparison with " and '
    By cowboy in forum New To Java
    Replies: 5
    Last Post: 01-24-2010, 05:33 PM
  3. String Comparison
    By evant8950 in forum Java Applets
    Replies: 6
    Last Post: 04-22-2009, 08:11 AM
  4. Ip adresses comparison
    By ModestUrgell in forum New To Java
    Replies: 0
    Last Post: 05-30-2008, 12:13 AM
  5. Cant get the logic right
    By jermaindefoe in forum New To Java
    Replies: 4
    Last Post: 03-11-2008, 12:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •