Take Array Input in Java

We can get array input in Java from the end-user or from a method. First, we will develop a program to get array input from the end-user through the keyboard, and later we will develop a Java program to take an array as an argument.

Prerequisite:- Array in Java

Java Program to Get Array Input From End-user

import java.util.Scanner;
class Test{
  public static void main(String[] args) {

    // Scanner class object to read input
    Scanner scan = new Scanner(System.in);

    // declaring and creating array objects
    int[] arr = new int[5];

    // displaying default values
    System.out.println("Default values of array:");
    for (int i=0; i < arr.length; i++) {
      System.out.print(arr[i]+"\t");
    }

    // initializing array
    System.out.println("\n\n***Initializing Array***");
    System.out.println("Enter "+ arr.length
                     + " integer values:");

    for(int i=0; i < arr.length; i++) {
      // read input
      arr[i] = scan.nextInt();
    }
    System.out.println("***Initialization completed***\n");

    //displaying initialized values
    System.out.println("Array elements are:");
    for (int i=0; i < arr.length; i++) {
      System.out.print(arr[i]+"\t");
    }

  }
}

Output:-

Default values of array:
0 0 0 0 0
***Initializing Array***
Enter 5 integer values:
10
20
30
40
50
***Initialization completed***
Array elements are:
10 20 30 40 50

array input in Java

In this array programs in java, array elements are not initialized with explicit values, they are initialized with a default value. The default value of the int data type is 0, so they are initialized with 0. Later we update array elements with values 10, 20, 30, 40, and 50.

Passing array as argument

To pass array object as an argument the method parameters must be the passes array object type or its super class type.

Program2:- Develop a Java program to define a method to receive number of integer values dynamically from another method as argument in this method. Read and display all values.

class ArrayMethod{

  public static void main(String[] args) {
    int[] array={5, 10, 20, 30, 55};
    display(array);
  }

  static void display(int[] a) {
    for(int i=0; i< a.length; i++) {
      System.out.print(a[i]+"\t");
    }
  }

}

Output:-

5 10 20 30 55

Program3: Develope a program to find sum of array elements. Display array elements, and use a user defined method for finding the sum of array elements.

class ArraySum{

  // main method
  public static void main(String[] args) {
    // array with explicit values
    int[] array = {5,15,25,35,45,55,65};
    int sum = 0;
    sum = sumOfArrayElements(array);
    System.out.println("\nSum of"
       + " array elements: " + sum);
  }

  // method to display array elements
  // and calculate sum
  static int sumOfArrayElements(int[] a) {
    int sum=0;
    // display array and calculate sum
    for(int i=0; i< a.length; i++){
      System.out.print(a[i]+"\t");
      sum += a[i];
    }
    return sum;
  }

}

Output:-

5 15 25 35 45 55 65
Sum of array elements: 245

When we pass an array object as an argument into a method, and if we modify array object values with method parameters then the modification is effected to the original referenced variable.

Q) Guess the output of the below program?

class Array{

  public static void main(String[] args) {
    int[] a = {10,20,30,40};
    m1(a);
    for(int i=0; i < a.length; i++){
      System.out.print(a[i]+"\t");
    }
  }

  static void m1(int[] ia){
    ia[1] = 4;
    ia[2] = 5;
  }
}

You may think the output 10, 20, 30, and 40 but it is wrong output. The actual output is:- 10 4 5 40; The Java language uses pass by reference not the pass by value. That’s why we get this output.

Program description:- Develop a Java program to read an array of double data-type values from the end-user. Pass this array to a method to calculate the sum of the array elements. Also, pass this array to a method to display the array elements and later display the sum of the array elements.

import java.util.Scanner;
class Test{

  // method to display array elements
  public static void display(double[] a) {
    for (int i=0; i < a.length; i++) {
      System.out.print(a[i]+"\t");
    }
  }

  // method to find sum of array elements
  public static double sumOfArray(double[] a) {
    double sum = 0.0;
    for (int i=0; i < a.length; i++) {
      sum += a[i];
    }
    return sum;
  }

  // main method
  public static void main(String[] args) {

    // Scanner class object to read input
    Scanner scan = new Scanner(System.in);

    // declare variables
    int n = 0;
    double[] arr = null;
    double sum = 0.0;

    // read number of elements
    System.out.print("How many numbers" +
             " you want to enter:: ");
    n = scan.nextInt();
    // declare array
    arr = new double[n];

    // initializing array
    System.out.println("Enter "+ arr.length
                     + " numbers:");
    for(int i=0; i < arr.length; i++) {
      // read input
      arr[i] = scan.nextDouble();
    }

    // pass array to find sum
    sum = sumOfArray(arr);

    // display array
    System.out.println("Array elements are:");
    display(arr);

    // display sum value
    System.out.println("\nSum = " + sum);
  }
}

Output:-

How many numbers you want to enter:: 5
Enter 5 numbers:
12.5
19.8
10
20
58
Array elements are:
12.5 19.8 10.0 20.0 58.0
Sum = 120.3

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

1 thought on “Take Array Input in Java”

Leave a Reply