How to Take Multiple String Input in Java Using Scanner17 Mar 2025 | 3 min read In Java, Scanner is a class that provides methods for input of different primitive types. It is defined in java.util package. In this section, we will learn how to take multiple string input in Java using Scanner class. We must import the package before using the Scanner class. For example, if want to take input a string or multiple string, we use naxtLine() method. It is only a way to take multiple string input in Java using the nextLine() method of the Scanner class. Java nextLine() MethodThe nextLine() method moves the scanner down after returning the current line. It reads String input including the space between the words. It does not accept any parameter. It returns a String (line) of that was skipped. Syntax: The method throws the NoSuchElementException if it does not find a line and throws IllegalStateException if the Scanner is closed. Let's create a program that takes multiple String input. In the following example, we have created an instance of the Scanner class. After that, we have invoked the nextLine() method that takes the string as input. At last, with the help of a for-each loop, we have printed all the strings that we have entered. MultipleStringInputExample1.java Output: Please enter the number of strings you want to enter: 5 Robert Harry Charlie Thomas David You have entered: Robert Harry Charlie Thomas David Let's see another example. In the following example, we have used the Scanner class. But did not use the nextLine() method. Instead of it, we have used forEachRemaining() method. It is a Java Interface Spliterator that is used to perform the specified action for each element sequentially in the current thread. It executes until all elements have been processed or the action throws an exception. It does not return anything. Syntax: It throws NullPointerException if the action is null. MultipleStringInputExample2.java Output: Enter the elements: Java Java Python Python Pascal Pascal C++ C++ Using Lambda ExpressionWe can also write the above program in the following manner by using the lambda expression. MultipleStringInputExample3.java Output: Canada Canada America America Russia Russia France France The following example also takes the multiple String input. But in this example, we have used another method hasNextLine(). The method returns true if there is another line of input of this scanner. The while loop execute until the method hasNextLine() returns false. MultipleStringInputExample4.java Output: Apple Apple Grapes Grapes Watermelon Watermelon In the following example, we have used a for loop that executes up to n times. Inside the loop, we have called the nextLine() method that takes the String input. MultipleStringInputExample5.java Output: Enter the elements: Data Structure Data Structure Web Services Web Services Web Designing Web Designing |
In this section, we will learn about the left view of a binary tree in Java with different approaches to achieve it. In the left view of a binary tree, we print only those nodes of the binary tree that are visible when the binary tree...
4 min read
Array slicing works mainly in programming languages like Python and JavaScript, allowing developers to effortlessly remove specific array segments. However, due to the design of the language in Java, the concept of array slicing is not so simple. Despite the built-in slicing syntax, Java offers...
5 min read
Eclipse is the most popular IDE for Java application development. In order to use eclipse effectively, we must familiar with the workbench, concepts, and components of Eclipse IDE. Before moving ahead in this section, we will understand what is eclipse in Java, eclipse platform overview, and...
5 min read
Java is a popular programming language that is widely used for developing various types of applications. One of the common tasks in many applications is to get a date from a calendar. In this section, we will explore how to get a date from a calendar...
3 min read
Difference between () and Line() In Java, the Scanner class available in the java.util.package is one of the easiest ways for obtaining input of the primitive data types such as int, double, and strings. In competitive programming, time is a constraint, and using the Scanner class is...
4 min read
The Exception Handling is one of the most powerful mechanisms to handle the runtime errors so that the normal flow of the application can be maintained. In Java, exception is an abnormal condition. Java programming language defines various exceptions. In this section, we will discuss the...
3 min read
An important problem in graph theory is that of determining all paths of a directed graph running from one vertex to another. It is especially useful in such things as routing, decision making for optimal paths for a network, and in general multitude of uses in...
5 min read
In this section, we will learn how to create a Java program to display alternate prime numbers. Prime Number: A prime number is a number p such that whenever p divides ab, then either p divides a or p divides b. In other words, a number that...
2 min read
There are three different types of Java getInteger() methods which can be differentiated depending on its parameter. These are: Java Integer getInteger(String nm) Method Java Integer getInteger(String nm, int val) Method Java Integer getInteger(String nm, Integer val) Method 1. Java Integer getInteger(String nm) Method: The getInteger(String nm) method is a...
5 min read
The use of recursion to reverse a doubly linked list in Java requires an understanding of both the structure of a doubly linked list and the recursion process. A doubly linked list's nodes are composed of three components: a data field, a pointer to the node...
5 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India