KnowledgeBoat Logo

Arrays

Single Dimensional Array: Accepting User Input

ICSE Computer Applications



In this lesson, we will learn how to accept user input in an array. For this, we will write a very simple program which will accept n numbers from the user, store them in a single dimensional array and print the array along with the sum of all the numbers. This n i.e. how many numbers the user wants to store will also be provided by the user as an input.

Here is our program:

import java.util.Scanner;

public class KboatSDAUserInputDemo
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("How many numbers you want to input?");
        int n = in.nextInt();
        
        int arr[] = new int[n];
        System.out.println("Enter the numbers: ");
        for (int i = 0; i < n; i++) {
            arr[i] = in.nextInt();
        }
        
        long sum = 0;
        for (int i = 0; i < n; i++) {
            System.out.println("arr[" + i + "] = " + arr[i]);
            sum += arr[i];
        }
        System.out.println("Sum of all numbers = " + sum);
    }
}

We ask the user how many numbers he wants to input by using the Scanner class and store it in the int variable n. We have to store the n numbers that the user will enter into a single dimensional array. This means that the size of our array should be n. Just to clarify this point a little more, say the user wants to store 10 numbers, then we will create an array of size 10. If he wants to store 15 numbers, then we will create an array of size 15. For this program, the size of the array is determined at runtime. We can do this by specifying the size of the array as the variable n like this:

int arr[] = new int[n];

As we want to store integers, the datatype of the array is int. The name of the array is arr and the square brackets after arr specify that it is an array variable not a regular variable. We create the array using the new operator. new operator requires the type and size of the array which we have specified as int and n, respectively.

After creating the array, we accept the numbers from the user and store them in the array arr using the below for loop:

for (int i = 0; i < n; i++) {
    arr[i] = in.nextInt();
}

We are initializing i to 0 because the array index starts at 0 not 1. The condition of the loop is i less than n so the loop will run till the value of i becomes equal to n – 1. As soon as i becomes equal to n, the condition will become false and the loop will terminate. Inside the loop, we accept the number from the user and store it at the corresponding index of the array. The first number entered by the user is stored as the first element of the array. The second number is stored as the second element of the array and so on.

In the next part of the program, we iterate over all the elements of the array print them and compute their sum:

long sum = 0;
for (int i = 0; i < n; i++) {
    System.out.println("arr[" + i + "] = " + arr[i]);
    sum += arr[i];
}
System.out.println("Sum of all numbers = " + sum);

The variable sum accumulates the sum of all the numbers present in the array. The datatype of sum is long because the result of adding many int values may exceed the range of int and become greater than the maximum value that int can store. Inside the loop, we are adding arr[i] to sum and finally outside the loop, we print the sum.

Below is the output of the program for 8 numbers — 1, 3, 5, 7, 9, 11, 13, 15.

Accept user input in single dimensional array Java program output

Our program correctly stores these numbers in a single dimensional array and computes the sum of all the numbers.

PrevNext