0

I'm trying to make java program that user can fill arrays with numbers then he can hunt them by typing its value the problem is when I try to fill array it only fills with last one typed. Can someone help me figure it out ?

package methods;


import java.util.Arrays;
import java.util.stream.IntStream;

public class HuntTheNumbers {

    public void huntingNumbersInArray(int[] ints, int userNumbersInput) {

        boolean contains = IntStream.of(ints).anyMatch(x->x==userNumbersInput);

        if (contains) {
            System.out.println(userNumbersInput+" is found");
        }else {
            System.out.println("value not found");
        }

    }
public void fillArray(int[] ints, int userNumbersToFillArray){

    Arrays.fill(ints, userNumbersToFillArray);
}
}

    import methods.CombineNumbers;
    import methods.HuntTheNumbers;
    
    import java.util.Arrays;
    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            HuntTheNumbers huntTheNumbers = new HuntTheNumbers();
            boolean start = true;
            int[] ints = new int[6];
            int[] firstArrayToMerge = new int[4];
            int[] secondArrayToMerge = new int[4];
            Scanner scanner = new Scanner(System.in);
            while (start) {
    
                System.out.println("======Menu======");
                System.out.println("1:Hunt the numbers");
                System.out.println("2:Combine the numbers");
                System.out.println("3:Quit");
    
                String command = scanner.next();
                if (command.equalsIgnoreCase("1")) {
                    System.out.println("Enter total number of elements");
                    int userNumberLengthInput = scanner.nextInt();
                    if (userNumberLengthInput > ints.length) {
                        start = false;
                        System.out.println("You can't put that many numbers in this array max size is 5");
                    } else {
    
                        System.out.println("Enter " + userNumberLengthInput + " elements");
                        for (int i = 0; i < userNumberLengthInput; i++) {
    
                            int userNumbersToFillArray = scanner.nextInt();
                            Arrays.fill(ints, userNumbersToFillArray);
    
                        }
    
    
                        System.out.println("Enter request value");
    
                        int requestValue = scanner.nextInt();
                        huntTheNumbers.huntingNumbersInArray(ints, requestValue);
                    }
                } else if (command.equalsIgnoreCase("2")) {
                    System.out.println("Enter total elements in first array");
                    int totalElementsInFirstArray = scanner.nextInt();
                    if (totalElementsInFirstArray > firstArrayToMerge.length) {
                        System.out.println("You can't put that many elements max size is 9");
                        start=false;
                    } else {
                        System.out.println("Enter elements of first Array");
                        for (int i = 0; i < totalElementsInFirstArray; i++) {
    
                            int userNumbersToFillArray = scanner.nextInt();
                            huntTheNumbers.fillArray(firstArrayToMerge, userNumbersToFillArray);
                        }
                    }
                    System.out.println("Enter total elements in second array");
                    int totalElementsInSecondArray = scanner.nextInt();
                    if (totalElementsInSecondArray > secondArrayToMerge.length) {
                        System.out.println("You can't put that many elements max size is 9");
                        start=false;
                    } else {
                        System.out.println("Enter elements of second Array");
                        for (int i = 0; i < totalElementsInSecondArray; i++) {
    
                            int userNumbersToFillArray = scanner.nextInt();
                            huntTheNumbers.fillArray(secondArrayToMerge, userNumbersToFillArray);
                        }
    
                    }
    
                    CombineNumbers combineNumbers = new CombineNumbers();
                    combineNumbers.mergeArrays(firstArrayToMerge, secondArrayToMerge);
                } else if (command.equalsIgnoreCase("3")) {
    
                    start=false;
                }
            }
        }
    }
Spectric
  • 30,714
  • 6
  • 20
  • 43

2 Answers2

4

Problem

The Arrays.fill() function fills your array entirely up with the same value which you pass as second argument. In your case the value of userNumbersToFillArray

Here an Example:

public static void main(String[] args) 
    { 
        int ar[] = {2, 2, 1, 8, 3, 2, 2, 4, 2}; 
  
        // To fill complete array with a particular 
        // value 
        Arrays.fill(ar, 10); 
        System.out.println("Array completely filled" + 
                  " with 10\n" + Arrays.toString(ar)); 
    } 
} 

Outputs:

Array completely filled with 10
[10, 10, 10, 10, 10, 10, 10, 10, 10]

Solution

To solve this you can simply assign inside your loop the user-input value to an array element by its indice.

Write instead of

 Arrays.fill(ints, userNumbersToFillArray);

This here

ints[i] = userNumbersToFillArray;

Here you can read more about Arrays.fill()

Aalexander
  • 4,987
  • 3
  • 11
  • 34
0

According to the docu Arrays.fill()

"Assigns the specified int value to each element of the specified array of ints."

So you are filling and array with e.g 5s ([5,5,5,5]).

And after the next Input you are doing the exact same with another number ({4,4,4,4}), hence why you end up with an array full of the last input.

Instead try writing every index of the array serparately like this:

public static void main(String[] args) {
    int[] ints = new int[5];
    Scanner scanner = new Scanner(System.in);

    //Foreach index of the array ask the user for a number.
    // And then change that index to the input
    for(int index= 0; index < ints.length; index++){
        System.out.println("Add a new number: ");
        int newInt = scanner.nextInt();
        ints[index] = newInt;
    }
    //Just print all values
    System.out.println(Arrays.toString(ints));
}
Jan
  • 65
  • 5