Results 1 to 2 of 2
  1. #1
    SamJava_the_Hut is offline Senior Member
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    100
    Rep Power
    0

    Default How to cherry-pick specifiied elements from one array to copy them to another array?

    The other java programming forums is down, so I hope you guys do not mind me re-asking my question here Basically, I want the findAndStoreValues function to allow a user to choose a value to look for in an existing array, and every time one is found, copy it to an empty array. Here's my code:

    SamsArrays.java
    Java Code:
    package samsExperiments;
    import java.util.Arrays;
    import java.util.Scanner;
    
    public class SamsArrays {	
    	
    	public int[] findAndStoreValues(int[]arr, int[]valuesFound) {
    		System.out.println("What integer value are you looking for:");
    		Scanner input = new Scanner(System.in);
    		int value = input.nextInt();
    		int j = 0;
    		for(int i = 0; i < arr.length; i++) {
    			if(i == value) {
    				System.out.println("Found a " + value + ". Copying it to the valuesFound array.");
    				arr[i] = valuesFound[j];
    				j++;
    			}
    		}
    		if(valuesFound == null) {
    			System.out.println("No " + value + " values were found to copy. valuesFound array is empty.");
    		}
    		return valuesFound;
    	}	
    }
    SamsExperimentsMain.java
    Java Code:
    package samsExperiments;
    
    import java.util.Scanner;
    import java.util.Arrays;
    import SortingAlgorithms.*;
    import customExceptions.BuiltInExceptions;
    
    public class SamsExperimentsMain {
    	
    	public static void main(String[] args){
    		
    		SamsArrays x = new SamsArrays();
    		int[] arr = {17,41,3,5,22,54,6,29,3,13};		
    		System.out.println("We start out with: " + Arrays.toString(arr));
    		
    		int[] valuesFound = new int[10];
    		valuesFound = x.findAndStoreValues(arr, valuesFound);
    		System.out.println("Our valuesFound array now has: " + Arrays.toString(valuesFound));		
    		
    	}//end of main method	
    	
    }//end of class

    The resulting output of the main method looks like this:


    We start out with: [17, 41, 3, 5, 22, 54, 6, 29, 3, 13]
    What integer value are you looking for:
    3
    Found a 3. Copying it to the valuesFound array.
    Our valuesFound array now has: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]


    In this code run, I chose to look for the value 3. If you look at the arr array, we have two 3's, so why do we not see "Found a 3. Copying it to the valuesFound array." printed out twice, and why is my valuesFound array still all zeros, instead of becoming "[3, 3, 0, 0, 0, 0, 0, 0, 0, 0]"? Am I missing something?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How to cherry-pick specifiied elements from one array to copy them to another arr

    Java Code:
      if(i == value) { // compare array index to user's input
    What is the purpose of testing the value in the array index variable (i)?

    Our valuesFound array now has: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    Where does the code change any elements in the valuesFound array?

    To see what is happening, print out the contents of arr after the call to findAndStoreValues.


    Also posted at: http://www.javaprogrammingforums.com...-know-why.html
    Last edited by Norm; 06-19-2019 at 04:27 PM.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 3
    Last Post: 11-25-2011, 12:38 AM
  2. Pick a random value from array?
    By RattleSnake in forum New To Java
    Replies: 2
    Last Post: 04-26-2011, 05:36 AM
  3. Pick random array index and change the element value
    By Space Ranger in forum New To Java
    Replies: 8
    Last Post: 03-29-2011, 10:18 PM
  4. Need help copy array into new array
    By Get_tanked in forum New To Java
    Replies: 2
    Last Post: 02-07-2011, 03:45 AM
  5. Replies: 16
    Last Post: 07-08-2010, 09:13 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
  •