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 2 of 2

Thread: My copied array is empty, and I don't know why.

  1. #1
    Member
    Join Date
    Dec 2018
    Location
    Wisconsin
    Posts
    54
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default My copied array is empty, and I don't know why.

    Doing some array experimenting, I attempted to to copy each element matching the value a user is looking for from the "arr" array to the "valuesFound" array Here's my code:

    SamsArrays.java
    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
    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 output 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]


    As you can see, the arr array we started out with has two 3's in it, but it doesn't even seem to go through the whole arr array, because we should be seeing, "Found a 3. Copying it to the valuesFound array" printed out twice, not once. What's more, the valuesFound array should have become "[3, 3, 0, 0, 0, 0, 0, 0, 0, 0]". What happened?

  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: My copied array is empty, and I don't know why.

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

       if(i == value) {   // compares value of array index to user's input
    What is the purpose of comparing user's input against the array's index value. Shouldn't the code be looking at the values in the arr array?
       if(arr[i] == value) {   // compare value in array with user's input
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Removing hyperlink of copied text while pasting in Jtexpane
    By arushee in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 21st, 2014, 08:22 AM
  2. Replies: 1
    Last Post: March 3rd, 2013, 01:32 PM
  3. first empty location in an array!?
    By keep smiling in forum Collections and Generics
    Replies: 2
    Last Post: February 10th, 2012, 07:29 AM
  4. Replies: 4
    Last Post: July 15th, 2011, 06:31 AM
  5. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 02:41 AM