All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

How To Take String Array Input In Java

Last Updated : Mar 11, 2024

How To Take String Array Input In Java

In this article we will show you the solution of how to take string array input in java, Java doesn't have a straightforward method to accept input from arrays.

However by using the Scanner class’s function, we may accept input in the form of an array.

The user must be questioned about the array's length before we can accept input from it.

The user's input is then captured using a Java for loop, and the identical for loop is utilized as well to get the array's items.

Now move to the concept of how to take string array input in java.

Step By Step Guide On How To Take String Array Input In Java :-

Any primitive type can be used as an input, and the matching primitive type method can be used to take the input of an array's elements.

import java.util.Scanner;
public class Demo{
public static void main(String args[]){
    Scanner sc=new Scanner(;
    String[] arr=new String[4];
    System.out.println("Default values of given String array: ");
    for(int i=0; i<arr.length; i++){
    System.out.println("******Initializing array******");
    System.out.println("Enter "+arr.length+" string values");
    for(int i=0; i<arr.length; i++){
    System.out.println("\n******displaying array elements******");
    System.out.println("Entered Strings are");
    for(int i=0; i<arr.length; i++){
  1. Here, you can see how we created a Java application that demonstrates how to accept input String components of an arrays using a for loop.
  2. The package java.util.Scanner is imported in the first line of code.
  3. Java's util package contains a class called Scanner that is used to obtain input of primitive types such int, double, and so on as well as strings.
  4. After that, we develop the demo public class before launching the main function.
  5. The user input is then read using the scanner class.
  6. A new Scanner instance is created by the syntax Scanner input = new Scanner(; that identifies the input stream provided as a parameter.
  7. The string array is then declared and made. String creates a copy of a argument string by initialising a freshly generated String object to represent the same character set as the argument.
  8. We declare a variable, then show it using a for loop.
  9. Next, we determine the supplied array's default value.
  10. After that, we give the array its starting value.
  11. Then, we initialise the array once more using the for loop.
  12. A scanner nextline() method is then employed. It is a scanner. The Java Scanner class's nextLine() method reads a line of text from scanner object and returns it.
  13. Next, the array's components are shown.

Conclusion :-

Thus, we have successfully mastered the Java notion of accepting a string array as input.

We also discovered that there isn't a simple way to receive input from arrays in Java.

To take input in the form of such an array, we must use the function of the Scanner class.

Before we can receive input from the array, we must ask the user about the array's length.

A Java for loop is then used to record the user's input, and the same for loop is also used to retrieve the elements of the array.

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

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪