All TalkersCode Topics

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

How To Take Multiple String Input In Java Using Scanner

Last Updated : Mar 11, 2024

How To Take Multiple String Input In Java Using Scanner

In this article we will show you the solution of how to take multiple string input in java using scanner, you can make an array of strings and utilise a loop to read in each input separately in Java when using the Scanner class to accept multiple string inputs.

To begin, construct a Scanner object to read console input. Users should then be prompted to enter the desired number of strings.

To read in each line using the nextLine() function of the Scanner class and save it in the array, create a string array of that length and then use a loop to read in each string.

Now let's discuss the idea of utilising a scanner in Java to accept multiple string inputs.

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

import java.util.Scanner;
public class TestDemo
public static void main(String[] args)
Scanner ss = new Scanner(;
System.out.print("Type the character count in the box:");
String[] str1 = new String [ss.nextInt()];
for (int i = 0; i < str1.length; i++)
str1[i] = ss.nextLine();
System.out.println("You need to input a character");
for(String str: str1)
  1. You can see that we made a simple Java programme to demonstrate how to utilise the Scanner class to read data from the user via the console.
  2. The maximum character count that the user wants to submit is requested before the programme constructs an array of strings that length and requests the user to input every characters separately.
  3. In the first line of code, the Scanner class from the Java.util package is imported.
  4. This class makes it possible for the software to read user input from the console.
  5. The next line defines a class called "TestDemo".
  6. The "main" method of this class serves as the start-up procedure for the programme.
  7. Then, in the main method, a new instance of the Scanner class is created and assigned to a variable called "ss".
  8. The InputStream to read from is supplied as an argument to the constructor of the Scanner class and represents input from console as the stream.
  9. The script will then prompt the user using the "System.out.print" mechanism to enter the desired number of characters.
  10. The input value, which can be obtained by calling the nextInt technique of the Scanner class, is the length of an array of strings referred to as "str1."
  11. The function "sc.nextLine()" receives the next line of data from the console and discards it because the nextInt technique does not use a newline character from user input.
  12. Then a loop is initiated, running for the length of the "str1" array.
  13. Every time the loop iterates, the nextLine function of the class Scanner is used, which prompts the user for a string before reading it and putting it in the current position of the "str1" array.
  14. Once all of the strings have been entered, the programme outputs a message requesting a character from the user.
  15. Next, a for-each loop is implemented to output each string on a distinct line after iterating through the "str1" array with loops.

Conclusion :-

As a result, we have successfully learnt how to use a scanner to accept multiple string inputs in Java.

We also discovered that this programme shows how to read data from the console and save it in an array of strings using the Scanner class.

It also shows how to alter & output the input data using loops and arrays.

I hope this article on how to take multiple string 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 🡪