How To Take String Array Input In Java
Last Updated : Mar 11, 2024
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(System.in); 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(arr[i]+"\t"); } System.out.println(""); System.out.println("******Initializing array******"); System.out.println("Enter "+arr.length+" string values"); for(int i=0; i<arr.length; i++){ arr[i]=sc.nextLine(); } System.out.println("\n******displaying array elements******"); System.out.println("Entered Strings are"); for(int i=0; i<arr.length; i++){ System.out.println(arr[i]+"\t"); } } }
- 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.
- The package java.util.Scanner is imported in the first line of code.
- 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.
- After that, we develop the demo public class before launching the main function.
- The user input is then read using the scanner class.
- A new Scanner instance is created by the syntax Scanner input = new Scanner(System.in); that identifies the input stream provided as a parameter.
- 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.
- We declare a variable, then show it using a for loop.
- Next, we determine the supplied array's default value.
- After that, we give the array its starting value.
- Then, we initialise the array once more using the for loop.
- 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.
- 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.