Array element help please im desperate
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, when I print out any element of the new array they are all still set to the default 0. This is what I have so far. Thanks.
Code:
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;
//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;
check(array[i],array,array2);
}
System.out.println(array2[0]);
}
public static int[] check(int num2,int[] array,int[] array2)
{
boolean results;
for (int j=0;j<array.length;j++)
{
int count=0;
results=search(array2 ,array[j],count);
if(!results)
{
array[j]=array2[count];
count++;
break;
}
}
return array2;
}
//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;
}
}
Re: Array element help please im desperate
Please post your code in [code] tags [/code] to retain its formatting.
Stick some debugging in there, printing out the various 'count' values you are using, and the search results.
That should highlight where you're going wrong.
(Though it might be obvious with formatted code).
Re: Array element help please im desperate
Sorry didn't know I had to do that this is my first post, that's for the help I have figured out that my count is not increasing but I'm not sure why so I'm still getting 0 for all elements. I edited the post also and tried to clean it up a bit. Any ideas why my count isn't increasing. Thanks.
Re: Array element help please im desperate
If you edited it you could have added in the tags...
I can't follow unformatted code.
Re: Array element help please im desperate
I didn't figure it out that those tags had to go on either side of the code till just now. But I got it fixed it was because I had Code:
array[j]=array2[count];
instead of Code:
array2[count]=array[j];
and also the breaks in my if blocks were messing it up . thanks for the help though