Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: problem copying array elements

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default problem copying array elements

    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; 
     
     
       }
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: problem copying array elements

    it wont let me put it in there
    Please explain what happens. What does "it won't let me" mean?

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    One problem I see is here:
    if(results=false);
    = is the assignment operator
    == is the compare for equality operator
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem copying array elements

    sorry didn't know how to do that but so far I've figured out that the issue was the breaks in the if statements ending the for loops cause my count to not count and that I had
    array[j]=array[count];
    backwards. But I do have another question if you don't mind helping I changed the code to this and everything works out better because I was suppose to have 2 separate methods one the copy the array element and one to search the array to see if it needs to be copied. But now since my count is in my SaveDistinctNumbers method when I try to print out the elements that were added to array2 in my main method I don't have the count which I need to tell array2 how many elements to print since I don't want to print the elements that weren't changed from the default 0. is there a way I could return array2 and count both from the SaveDistinctNumbers method?
    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;
                SaveDistinctNumbers(array[i],array,array2,count);
             }
     
             System.out.println(count);
     
          }
     
          public static int[] SaveDistinctNumbers(int num2,int[] array,int[] array2,int count)
          {
             boolean results;
     
             for (int j=0;j<array.length;j++)
             {
                int count=0;
                results=search(array2 ,array[j],count);
                if(!results)
                {		   
                   array2[count]=array[j];
                   count++;
     
     
     
              }
     
     
              } 
     
     
     
             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;
     
                }
             }
             return found; 
     
     
          }
     
     
     
       }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: problem copying array elements

    is there a way I could return array2 and count both
    You don't have to return array2. It is not a newly created array. It was passed to the method as an arg.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem copying array elements

    im trying to create a for loop in the main method after all the elements have been saved that looks like this but im not getting any output because in the main method count is still equal to 0.
    for(int i=0;i<count;i++)
    {
    System.out.println(array2[i]);
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: problem copying array elements

    count is still equal to 0.
    Where is a value assigned to count? Is that value 0?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem copying array elements

    I have count declared at 0 at the top of my main method I must have missed that when I was copying it. All Im trying to be able to do is print out array2 up until the element where nothing else was added which i would think shouldn't that hard but for some reason I just cant get it.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: problem copying array elements

    Where is the number of elements in the array computed and saved so it can be used to control the loop?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Feb 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem copying array elements

    that's what count does so in my method to save the numbers count is set to 0 . the user enters a number and that is saved into array 1 wether it is a number the user already entered or not then i call the method to SaveDistinctNumber which calls my search method and if the search method finds that the number has already been entered the number isn't added to the second array , but if the number hasn't been entered yet it returns false which causes SaveDistintNumber to add the number to the new array and increment count by 1.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: problem copying array elements

    count is still equal to 0.
    increment count by 1.
    If count has the number of elements in the array, can you explain how its value is 0?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Feb 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem copying array elements

    because it is set to 0 inside the SaveDistinctNumber method doesn't increment until it is inside the for loop in the SaveDistinctNumber method that's why i was talking about returning count to the main method but when I return it it goes into the for loop that calls that method which is a separate loop then the loop that needs to controlled by the count

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: problem copying array elements

    returning count to the main method
    If a non-zero value is returned by the method and saved in the count variable, then the value of count is not zero. How does the value of count get set to zero after is has gotten a non-zero value that was returned by the method?

    If you can't see where the code sets the value of count back to zero, you will have to post the code.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Array elements comparison
    By Pradeep_Parihar in forum Java Theory & Questions
    Replies: 1
    Last Post: December 10th, 2011, 09:45 AM
  2. [SOLVED] Problem copying an arraylist to an array
    By allhalf425 in forum Collections and Generics
    Replies: 4
    Last Post: September 20th, 2011, 10:25 AM
  3. copying 2dim array into 1 dim array
    By av8 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: June 29th, 2011, 10:59 PM
  4. Replies: 2
    Last Post: May 6th, 2011, 05:19 PM
  5. Copying Array Problems.. Not what you think
    By xXRedneckXx in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 5th, 2011, 12:01 PM