Program to Take Multiple String Input in Java using Scanner

In the previous Java program, we had seen how to take string input in Java using scanner class. Now in this post, we will discuss how to take multiple String input in Java using Scanner. To store multiple String inputs we need a String array. See more:- String array in Java.

There are two different options:- read one word or read one line. In scanner class, we have the next() method which reads one word at a time, and nextLine() method which reads one line at a time.

MethodUses
next()To read one word.
nextLine()To read one line at a time.

While reading string value generally we prefer to read one line therefore we will focus on the nextLine() method. The nextLine() method is declared as follows:- public String nextLine()

Let us demonstrate it through a Java program:- How to Take Multiple String Input in Java using Scanner?

import java.util.Scanner;

public class Test {

   public static void main(String[] args) {
      // create Scanner class object
      Scanner scan = new Scanner(System.in);

      // create String array of size 5
      String lang[] = new String[5];
      
      // read input
      System.out.println("Enter 5 Language Name:");
      for (int i = 0; i < 5; i++) {
         lang[i] = scan.nextLine();
      }

      // display
      System.out.println();
      System.out.println("Programming languages are:");
      for (int i = 0; i < 5; i++) {
         System.out.println(lang[i]);
      }
   }
}

The output of the above program:-

Enter 5 Language Name:
Java
JavaScript
C
C++
Python

Programming languages are:
Java
JavaScript
C
C++
Python

In this program, first, we created a Scanner class object. Then we created a String array of size 5 to store five-string values. Using loop and nextLine() method this program read 5 string value and stored into the string array. After that, using loop string elements are fetched and displayed to the screen.

Let us see another program where multiple Strings are taken as input from the end-user.

import java.util.Scanner;

public class Test {

   public static void main(String[] args) {
      // create Scanner class object
      Scanner scan = new Scanner(System.in);

      // read input
      System.out.print("Enter Name: ");
      String name = scan.nextLine();

      System.out.print("Enter programming langauge: ");
      String lang = scan.nextLine();

      System.out.println("Enter 5 other language name:");
      String str[] = new String[5];
      for (int i = 0; i < 5; i++) {
         str[i] = scan.nextLine();
      }

      // display
      System.out.println();
      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++) {
         System.out.println(str[i]);
      }
   }
}

The output of the above program:-

Enter Name: Rocco
Enter programming langauge: Java
Enter 5 other language name:
C
C++
Python
PHP
JavaScript

Name: Rocco
Current programming language: Java
Other languages are:
C
C++
Python
PHP
JavaScript

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!

Leave a Comment

Your email address will not be published. Required fields are marked *