0

I have to make an array of 10 numbers then copy that array into a new array without the duplicates. I got it to the point where it will weed out dups but for some reason after I determine that a number is not already in the new array it wont let me put it in there. This is what I have so far. Thanks.

 import java.util.*;
 import java.io.*;
 public class PrintingDistinctNumbers
 {
   public static void main(String[] args)
   {
     int[] array=new int[10];
     int[] array2=new int[10];
     int num1;
     int count = 0;
     boolean results;
     //let the user input 10 numbers
     for(int i=0;i<array.length;i++)
     {
       Scanner input=new Scanner(System.in);
       System.out.println("please enter 10 numbers");
       num1=input.nextInt();
       array[i]=num1;
     }

     for (int j=0;j<array.length;j++)
     {
       results=search(array2 ,array[j],count);
       if(results=false);
       { 
         array[j]=array2[count];
         count++;
         break;
       }

     }
     // just a test to make sure the numbers copied over to the new array 
     System.out.println(array2[0]);
   }



   //search the second array to see if the int is allready in it 
   public static boolean search(int[] array2,int value,int count2)
   {
     //create variables
     boolean found;
     //set the variables
     found= false;
     //search the array
     for(int index=0;index<count2;index++)
     {
       if(array2[index]==value)
       {
         found=true;
         break;
       }
     }
     return found; 


   }

 }
1
  • I also tried moving the count declaration inside the loop that calls the search method but I am still getting the default 0 for every element in array2 Commented Feb 26, 2013 at 10:30

3 Answers 3

4

Without looking at the rest of your logic, this

 if(results=false);

doesn't look right

  1. is that a typo ? You need if (results == false), or more concisely, if (!results)
  2. note the trailing semicolon, which means the following block will execute regardless of what your if clause evaluates to. The ; is creating an empty block, which is entierely valid.
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the comment I didn't notice that but I'm still getting a print of 0 when I try and print out any element of array2 after entering my integers , even after the fix.
0

Besides if (results=false) already mentioned by Brian Agnew, I see this:

  array[j]=array2[count];

You overwrite the values in the array, in which you stored your input with the values of an uninitialized second array. You probably meant to do

  array2[count] = array[j];

here.

Comments

0

There are two bugs:

  1. The break; statement in the if block should not be there: That would break you out of the loop, but you need the loop to keep iterating over the array until all the elements have been copied.
  2. The test is assigning false to result, not comparing it, so change to if (!result)

There are a few style issues too:

  1. Your search method is waaaay to long; you don't need the found variable
  2. Name your parameters with what makes sense within the method: you have array2 when there's no array or array1 in scope. Same for count2
  3. Prefer i to index for the loop var - it's just less to type and less to read

This is more like what it should look like:

public static boolean search(int[] array, int value, int count) {
  for(int i = 0; i < count; i++) {
    if (array[i] == value) {
      return true;
    }
  }
  return false;

}

In your main method, why do you have one loop with i and the next with j? Make them both i - the loop variable only has scope within the loop, so there's no clash.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.