Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Tolls

Thread: Array element help please im desperate

  1. #1
    dendoc01 is offline Member
    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0

    Default 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.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default 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).
    dendoc01 likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    dendoc01 is offline Member
    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0

    Default 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.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default 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.

    ** This space for rent **

  5. #5
    dendoc01 is offline Member
    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0

    Default 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
    Java Code:
    array[j]=array2[count];
    instead of
    Java Code:
    array2[count]=array[j];
    and also the breaks in my if blocks were messing it up . thanks for the help though

Similar Threads

  1. Trouble copying an array
    By xXRedneckXx in forum New To Java
    Replies: 10
    Last Post: 02-05-2011, 05:36 PM
  2. Replies: 11
    Last Post: 01-31-2011, 09:59 PM
  3. Copying ArrayList into an Array
    By Manfizy in forum New To Java
    Replies: 6
    Last Post: 07-16-2009, 07:03 AM
  4. Simple Project 4 or 5 Hours Pay $100
    By billaaa777 in forum Jobs Offered
    Replies: 0
    Last Post: 08-30-2008, 12:45 AM
  5. Seems so simple yet I can't figure it out!
    By adamhaviland in forum New To Java
    Replies: 1
    Last Post: 11-03-2007, 06:26 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
  •