Results 1 to 8 of 8
  1. #1
    baumboards is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Red face Printing repeating integers in a given array?

    I am given a set of integers in an array, I need to print out the repeating integers and their indices. Thus far my code prints out every single comparison, what I need it to do is to only print out the index of the values that do match.

    public class FindDuplicate {

    public static void main(String[] args) {

    int[] arr = {1, 2, 2, 3, 4, 2, 4, 3, 0, 5, 3, 2};

    int i,j;

    for( i = 0;i<arr.length;i++){
    for( j = i + 1; j<arr.length;j++){

    if(arr[i] == arr[j]);

    System.out.println("Index " + i +" same as Index "+j+" with value "+ arr[i]);

    }
    }
    }
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Use braces for every if statement - even those that execute a single line of code. That will help you catch problems like this.

    (If you take this advice and repost your code if it still doesn't behave as you expect, use the code tags. Put [CODE] at the start of the code and [/CODE] at the end. That way the formatting will be preserved.)

  3. #3
    baumboards is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    Your suggestion did work, by adding the braces around both the for-statements and if-statements the program now prints out only one output, albeit a wrong output, but still one output vs. many incorrect outputs. Here is the improved code that still doesn't work completely:
    Java Code:
    public class FindDuplicate {
    
    	public static void main(String[] args) {
    		 
    		int[] arr = {1, 2, 2, 3, 4, 2, 4, 3, 0, 5, 3, 2};
    		
    		int i,j=0;
    		{    
    		    for(i = 0;i<arr.length-1;i++)
    			{  for(j = i+ 1; j<arr.length;j++)
    				{
    			if(arr[i] == arr[j]);	
    		    
    				}
    			   }	
    		System.out.println("Index " + i +" same as Index "+j+" with value "+ arr[i]);	
    		
    			
    	}	
    	 }
          }

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Personally I find that even harder to read. I was suggesting using braces with the one line if statement. Putting the opening brace at the end of the line as you did originally you would get:

    Java Code:
    public class FindDuplicate {
    
        public static void main(String[] args) {
    
            int[] arr = {1, 2, 2, 3, 4, 2, 4, 3, 0, 5, 3, 2};
    
            int i,j;
    
            for( i = 0;i<arr.length;i++){
                for( j = i + 1; j<arr.length;j++){
    
                    if(arr[i] == arr[j]);{ // <-- this is the line that's wrong
    
                        System.out.println("Index " + i +" same as Index "+j+" with value "+ arr[i]);
    
                    }
                }
            }
        } 
    }

    The semicolon ; should not be there.

  5. #5
    baumboards is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    Haha leave it to a java veteran to solve my problem with the simplest of solutions. The code works just fine now. How is it that a semi-colon made all the difference?

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    How is it that a semi-colon made all the difference?

    Java Code:
    if(arr[i] == arr[j]);
    {
        System.out.println("Index " + i +" same as Index "+j+" with value "+ arr[i]);
    }

    The first line here is an empty if statement. It is saying "if the two array values are equal do nothing".

    The one line block that follows forms a statement that is completely separate from the if statement. It will be executed whatever the value of arr[i]==arr[j] is, which is why you were seeing all that output.

    The reason I suggested using braces for the one line if statement is that a line like:

    Java Code:
    whatever);{

    is never what you mean. The ; ends one statement, and the { begins a new one. Since we don't put two statements on one line (or shouldn't) this should strike the eye as an error.

  7. #7
    baumboards is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Smile

    Thank you again for explaining that to me.

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

Similar Threads

  1. Replies: 7
    Last Post: 02-04-2010, 12:09 PM
  2. Non-Repeating Random Integers
    By Psyclone in forum New To Java
    Replies: 5
    Last Post: 01-31-2010, 10:04 PM
  3. file input: array of integers
    By hannes in forum New To Java
    Replies: 8
    Last Post: 01-27-2010, 04:44 PM
  4. Error if array contains duplicate integers
    By lithium002 in forum New To Java
    Replies: 4
    Last Post: 12-05-2009, 09:58 AM
  5. Replies: 1
    Last Post: 07-04-2008, 06:39 PM

Posting Permissions

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