Results 1 to 7 of 7
  1. #1
    trishtren is offline Member
    Join Date
    Feb 2011
    Posts
    53
    Rep Power
    0

    Unhappy inner loop problem

    Hello, im trying to iterate through my array to check if all values within the array are the equal, however the inner loop seems to produce 2 results that it should not to begin with, after which it is accurate:
    Java Code:
    public static void Arraychecker(){
    		int [] TestArray = { 1, 0 , 3, 4}; // int array with 4 elements
    		
    	
    		for (int i = 0;i < TestArray.length;i++)
    		{
    			
    			//System.out.println("j is"+i);
    			for(int j = 0;j < TestArray.length;j++)
    			{
    				if (TestArray[i]==TestArray[j++])
    				{
    					System.out.println("i = j");
    				}
    
    			}
    		}
    
    	
    	}
    The first test results in i = j being printed twice even when it is not true. Any help solving this would be grand :)
    Last edited by trishtren; 10-08-2011 at 02:51 PM.

  2. #2
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    5

    Default Re: inner loop problem

    You're testing if TestArray[i] is equal to TestArray[j++]. j++ is not a variable but j is.

  3. #3
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    200
    Rep Power
    5

    Default Re: inner loop problem

    why do you have return 0? Why not put void as return type?

  4. #4
    trishtren is offline Member
    Join Date
    Feb 2011
    Posts
    53
    Rep Power
    0

    Default Re: inner loop problem

    good point, but is more or less irrelevant to the problem im trying to solve. thanks anyway.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,050
    Blog Entries
    7
    Rep Power
    23

    Default Re: inner loop problem

    Both i and j run over all index values of your TestArray independently; of course TestArray[0] == TestArray[0] and the same holds for the values 1, 2 and 3 ...

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  6. #6
    trishtren is offline Member
    Join Date
    Feb 2011
    Posts
    53
    Rep Power
    0

    Default Re: inner loop problem

    this makes sense, but im trying to compare TestArray[0] against TestArray[1] basically, so i always want to compare 1 value infront of the current one, any idea how i can make this happen correctly?

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,050
    Blog Entries
    7
    Rep Power
    23

    Default Re: inner loop problem

    Quote Originally Posted by trishtren View Post
    this makes sense, but im trying to compare TestArray[0] against TestArray[1] basically, so i always want to compare 1 value infront of the current one, any idea how i can make this happen correctly?
    You have to fiddle a bit with the indexes i and j; you want their values to be, (0, 1) (0, 2) (0, 3) (1, 2) (1, 3) (2, 3); that should ring a bell ...

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. While loop problem
    By coderon in forum New To Java
    Replies: 1
    Last Post: 07-12-2011, 01:12 AM
  2. Problem with a for loop
    By ile4 in forum New To Java
    Replies: 3
    Last Post: 12-02-2010, 03:23 PM
  3. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 07:12 AM
  4. Help with a loop-like problem
    By Jnoobs in forum New To Java
    Replies: 1
    Last Post: 10-14-2009, 01:15 AM
  5. Loop Problem
    By jralexander in forum New To Java
    Replies: 4
    Last Post: 12-02-2008, 08:08 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
  •