All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Take Array Input In Java

Last Updated : Mar 11, 2024

Take Array Input In Java

In this article we will show you the solution of take array input in java, as usual first we need to import package in case you are using eclipse then all program will import by itself during creation.

As we know array needs to have more than two values because its like container to hold more values. To read or write array you need loop support then you can take input from users through scanner object.

Step By Step Guide On Take Array Input In Java :-

In main class of ‘ReadArr_prgm’ we given main method definition. To request end user we created scanner object 'sc' then we declared integer array with size 3.

To get success reading process you need to appends scanner object with array variable.

It will return the user input to empty array, but remember either read or write you need loop. So here we used for loop above reading line code as well as above println() method.

//Take Array Input
package java_prgms;
import java.util.*;
class ReadArr_prgm
{
 public static void main(String args[])
    {
  Scanner sc=new Scanner(System.in);
  int a[]=new int[3];
        System.out.println("Enter 3 Numbers For Array");
        for(int i=0;i<a.length;i++)
        a[i]=sc.nextInt();
        sc.close();
        System.out.println("User Entered Array Values");
        for(int i=0;i<a.length;i++)
        System.out.println(a[i]);
     }
}
  1. The ‘Package java_prgms’ may differ as per developer definition and its container name where your overall data stored it. Actually it’s a namespace that organizes a set of related classes and interfaces.
  2. In main class ‘ReadArr_prgm’ we given public static void main method definition. There first we created scanner object 'sc' and defined array variable 'a' with 3 size. Which means it can store up to 3 integer values.
  3. To intimate users to give input for array variable we displaying notify message 'Enter 3 Numbers For Array'. Then we defined for loop with condition array length, so it will proceeds loop until reach array length.
  4. Within for you need to bind 'nextInt()' with scanner object 'sc'. It will read the input from user 3 times and storing collected input to array variable 'a'.
  5. After getting 3 integer from user we closing scanner object through 'sc.close()' statement. Then with similar process of requested input, we doing display process that will replicates at terminal.
  6. The ‘System.out.println()’ is used to print an argument that is passed to it. And which is separate into three parts such as System- final class in java, out-instance of PrintStream type that is a public and static member field of System class, println()-instance of PrintStream class hence we can invoke same on out.
  7. Ensure each brackets has closed with its pair perfectly at the end. Then every statement must end by ‘;’.

Conclusion :-

In conclusion now we are able to know how to take array input from user in java.

To compile java program you need to tell the java compiler which program to compile through ‘javac ReadArrr_prgm.java’.

After successful compilation you need to execute ‘java ReadArr_prgm‘ line for gets the output. Then you can see message 'Enter 3 Numbers For Array', now user needs to enter 3 integers.

For instance user gave 3 integers like 45, 2, 34 then you will get result as User Entered Array Values at next line 45, 2, 34 values.

On eclipse just press of run button then you will get output inside terminal. But might be output will differ based on user inputs in case you giving input same as above line then you get similar output.

I hope this article on take array input in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪