How to take number input in terminal (Java)

·

1 min read

I recently started studying Java and needed to record some notes that I would need to reference from time to time.

Following is a short code snippet on how to take numbers as input in terminal.

    private static int[] readIntegers(int count) {
        int[] array = new int[count];
        for(int i = 0; i < count; i++) {
            System.out.println("Enter a number: ");
            int number = scanner.nextInt();
            scanner.nextLine();
            array[i] = number;
        }
        return array;
    }