dendoc01
dendoc01

Reputation: 1

Simple array copying issue

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; 


   }

 }

Upvotes: 0

Views: 89

Answers (3)

Bohemian
Bohemian

Reputation: 425033

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.

Upvotes: 0

user1252434
user1252434

Reputation: 2121

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.

Upvotes: 0

Brian Agnew
Brian Agnew

Reputation: 272297

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.

Upvotes: 4

Related Questions