All TalkersCode Topics

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

How To Take String Array Input In Java Using Scanner

Last Updated : Mar 11, 2024

How To Take String Array Input In Java Using Scanner

In this article we will show you the solution of how to take string array input in java using scanner, 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.

We need to ask the user for the array's length before we can accept input from it.

The user's input is then taken into account using a Java for loop, which is also used to get the array's elements.

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.

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

A class called Scanner in the Java programming language offers methods for accepting input of various primitive types.

The java.util package includes a description of it. It is possible to take in an array of input from the user in Java by using loops as well as the Scanner class.

import java.util.Scanner;
public class MyTest {
    public static void main(String[] args) {
        Scanner sc = new Scanner(;
        System.out.print("Enter Name: ");
        String name = sc.nextLine();
        System.out.print("Enter programming langauge: ");
        String lang = sc.nextLine();
        System.out.println("Enter 5 other language name:");
        String str[] = new String[5];
        for (int i = 0; i < 5; i++) {
            str[i] = sc.nextLine();
        System.out.println("Name: " + name);
        System.out.println("Current programming language: " + lang);
        System.out.println("Other languages are:");
        for (int i = 0; i < 5; i++) {
  1. Here, you can see how we created a Java programme utilising the Scanner Class to accept string input.
  2. Import java.util.Scanner is written at the start of the code to import the package.
  3. To gather user input, the class Scanner with in java.util package is utilised.
  4. Next, a public class called mytest is created, and the main method is then called using the public static void main syntax.
  5. Then we design a scanner-related object. Creates a new instance of the Scanner class that relates to the input stream specified by
  6. Next, to print the text, we use system.out.println, which is Java's print function.
  7. Create a string type variable and declare it to contain the input value. We created the String type variables name and lang for this application.
  8. Next, we use a scanner.
  9. A line of text that was read from scanner object is returned by the Line() function of the Java Scanner class.
  10. The names of three further programming languages are then obtained using a for loop.
  11. After that, we print every piece of data we received from the user using the print function.
  12. And finally, we close the scanner using the scan close function.

Conclusion :-

As a result, we have successfully learnt how to use a scanner to take a string array as input in Java.

We discovered that we could receive input in the form of an array in Java by utilising the Scanner class' function. Before we can receive input from the user, we must first ask for the length of the array.

A Java for loop is then used to consider the user's input and to retrieve the members of the array.

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

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪