Q:

How can I store user inputs in an array of integers?

I do not know how many numbers the user will input, and I need the program to stop inserting the numbers in the array when it reaches the end of the line, because in the new line the user will input a number that is not related to this.
For exemple:
The user could input:
1 5 7 8 9 5
4
or:
5 4 8 9 4 2 1 3 2 4 
7
The array I need would be, for exemple 1: [1, 5, 7, 8, 9, 5]

A:

If your requirement allows you to ask the user, you can do it as follows:
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int[] num;
        Scanner scanner=new Scanner(System.in);
        System.out.print("How many integers you want to enter: ");
        int n = 0;
        if(scanner.hasNextInt()) {
            n=scanner.nextInt();
        }
        num=new int[n];
        for(int i=0;i<n;i++) {
            System.out.printf("Enter integer %d: ",i+1);
            if(scanner.hasNextInt()) {
                num[i]=scanner.nextInt();
            }           
        }
        System.out.println(Arrays.toString(num));
    }
}

A sample run:
How many integers you want to enter: 3
Enter integer 1: 10
Enter integer 2: 15
Enter integer 3: 20
[10, 15, 20]

You can also do it as follows:
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int[] num;
        Scanner scanner = new Scanner(System.in);
        System.out.print("How many integers you want to enter: ");
        int n = 0;
        if (scanner.hasNextInt()) {
            n = scanner.nextInt();
            scanner.nextLine();
        }
        num = new int[n];
        System.out.print("Enter the integers separated by a space: ");
        String[] strNums = null;
        if (scanner.hasNextLine()) {
            strNums = scanner.nextLine().split(" ");
        }
        if (strNums != null) {
            for (int i = 0; i < n; i++) {
                try {
                    num[i] = Integer.parseInt(strNums[i]);
                } catch (Exception e) {
                    System.out.println("Invalid input");
                    break;
                }
            }
            System.out.println(Arrays.toString(num));
        }
    }
}

A sample run:
How many integers you want to enter: 3
Enter the integers separated by a space: 10 15 20    
[10, 15, 20]

However, if you want the number of integers to be unrestricted, you should use a List instead of an array (as the size of an array is fixed at the time of initialization) as follows:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        List <Integer> intList=new ArrayList<Integer>();
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the integers separated by a space: ");
        String[] strNums = null;
        if (scanner.hasNextLine()) {
            strNums = scanner.nextLine().split(" ");
        }
        if (strNums != null) {
            for (String strNum: strNums) {
                try {
                    intList.add(Integer.parseInt(strNum.trim()));
                } catch (Exception e) {
                    System.out.println("Invalid input");
                    break;
                }
            }
            System.out.println(intList);

            //You can even get an array out of the list as follows:
            Integer[] nums = intList.toArray(new Integer[0]);
            System.out.println(Arrays.toString(nums));
        }
    }
}

A sample run:
Enter the integers separated by a space: 10 20 30 40
[10, 20, 30, 40]
[10, 20, 30, 40]

Wish you all the best!

