Results 1 to 5 of 5
- 02-26-2013, 11:05 AM #1
Member
- Join Date
- Feb 2013
- Posts
- 7
- Rep Power
- 0
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.
Java 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; } }Last edited by dendoc01; 02-26-2013 at 12:43 PM.
- 02-26-2013, 11:57 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
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).Please do not ask for code as refusal often offends.
- 02-26-2013, 12:33 PM #3
Member
- Join Date
- Feb 2013
- Posts
- 7
- Rep Power
- 0
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.
- 02-26-2013, 12:39 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: Array element help please im desperate
If you edited it you could have added in the tags...
I can't follow unformatted code.Please do not ask for code as refusal often offends.
- 02-26-2013, 01:01 PM #5
Member
- Join Date
- Feb 2013
- Posts
- 7
- Rep Power
- 0
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
instead ofJava Code:array[j]=array2[count];
and also the breaks in my if blocks were messing it up . thanks for the help thoughJava Code:array2[count]=array[j];
Similar Threads
-
Trouble copying an array
By xXRedneckXx in forum New To JavaReplies: 10Last Post: 02-05-2011, 05:36 PM -
array problem: simple (I'm sure), but defeating me
By rippon in forum New To JavaReplies: 11Last Post: 01-31-2011, 09:59 PM -
Copying ArrayList into an Array
By Manfizy in forum New To JavaReplies: 6Last Post: 07-16-2009, 07:03 AM -
Simple Project 4 or 5 Hours – Pay $100
By billaaa777 in forum Jobs OfferedReplies: 0Last Post: 08-30-2008, 12:45 AM -
Seems so simple yet I can't figure it out!
By adamhaviland in forum New To JavaReplies: 1Last Post: 11-03-2007, 06:26 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks