×

How to take Multiple String Input in Java using Scanner class

Scanner class is a class which takes the multiple input data or the single input data through an objects and methods. The Scanner class will be available in the java.util package.

In this article we are excited to learn how to read the multiple string input in the java using the Scanner class.

The Methods in the Scanner class which we invoke in the data types with respect to the methods:

       DATA TYPESMETHODS
Integer nextInt()
Floating point nextFloat()
Boolean   nextBoolean()
String nextLine()
Character next().charAt(0)

Syntax for creating the Scanner class

Classname objectname = new Classname(parameters);
Scanner sc = new Scanner();

Here we are using the Scanner as the Class name and sc as the object name.

Now we will see the data types with the examples in java.

For writing the programs we mainly use some data types. I.e., String data type, Integer data type, Floating data type, Character data type etc...

Java nextInt() Method:

The java nextInt() method moves the Scanner class to the next integer value. It reads the integer input data.

Java nextFloat Method:

The java nextFloat() method moves the current scanner class to the next Floating-point value. It reads the floating-point values.

Java nextBoolean() Method:

The java nextBoolean() method moves the current class to the next Boolean values. The scanner class reads the Boolean values as true or false.

Java nextLine() Method:

The java nextLine() moves to the current scanner class to the next line. The java scanner class reads the String input data including the spaces between the words.

Java next.charAt(0) Method:

The next.charAt(0) method is similar as the nextLine() method. The only difference is that it reads the character data and it doesn’t read the spaces between the words.

Examples

Let's see a simple example program for reading the person data using scanner class.

Example 1:

import java.util.Scanner;
public class Demo    // class name
{
public static void main(String[] args)   // main method
{
Scanner ss = new Scanner(System.in);  // scanner class
System.out.print("Enter the number of characters:");
String[] str1 = new String [ss.nextInt()];
sc.nextLine();// nextLine() Method
for (int i = 0; i < str1.length; i++)   // initializing for loop
{
str1[i] = ss.nextLine();
} //i
System.out.println("The character you have to entered");
for(String str: str1)// for-each loop syntax
{
System.out.println(str);
}// for
} // Demo
}// main

OUTPUT:

How to Take Multiple String Input in Java using Scanner

In the above example we consider the multiple string inputs. We used the methods like nextLine() for moving the next current line.

Example 2:

import java.util.Scanner;
public class Demo  // class name
{
public static void main(String[] args)// main method
{


int n=5;
System.out.println("Enter the elements: ");
Scanner sc = new Scanner(System.in);  // scanner class
for(int i = 0; i < n; i++)  // initializing for loop
{
String str1 = sc.nextLine();// nextLine() method
System.out.println(str1);
}
}
}

OUTPUT:

How to Take Multiple String Input in Java using Scanner

In the above java example, we taken the multiple string inputs. In this example we used the nextLine() method. This method it returns the true statement if the another nextLine() method of the scanner class is currently in use. In the following code the for loop executes until the method returns the false.

Example 3:

import java.util.Scanner;
public class Demo11  // class name
{
public static void main(String[] args)   // main method
{
Scanner scn = new Scanner(System.in);// scanner class
while(scn.hasNextLine())  //while loop with nextLine() method
{
System.out.println(scn.nextLine());
}
}
}

Output:

How to Take Multiple String Input in Java using Scanner

In the above java example, we taken the multiple string inputs. In this example we used the nextLine() method. This method it returns the true statement if the another nextLine() method of the scanner class is currently in use. In the following code the while loop executes until the method returns the false.

Example 4:

import java.util.Scanner;
public class Demo13  
{
public static void main(String[] args) 
{
String name;
int id, age;
Scanner sc = new Scanner(System. in );
System.out.println("please enter the person details");
System.out.println("Enter your name");
name = sc.nextLine();
System.out.println("Enter your id");
id = sc.nextInt();
System.out.println("Enter your age");
age = sc.nextInt();
System.out.println("You have entered the following person details:");
System.out.println("Name: " + name);
System.out.println("Id: " + id);
System.out.println("Age: " + age);
}
}

Output:

How to Take Multiple String Input in Java using Scanner

This is the basic scanner class program for the beginner level students. Here we are using two methods like nextLine() for the String inputs and second Method nextInt() for the integer inputs.


Related Topics

Java Iterator

When iterating through, traversing, or retrieving the individual elements of a Collection or Flow object, a Java Cursor is leveraged as an iterator. In Java, there exist three types of...

4 minutes read.

Java Transient Keyword

An object in Java can be turned into a stream of bytes using serialization. The data of the instance and the kind of data saved in that instance are both...

3 minutes read.

Interface Program in Java

Interface Program in Java In the previous topic, we discussed that abstraction is possible through the interface and abstract class. An abstract class provides partial to 100% abstraction. 100 %abstraction in...

4 minutes read.

Binary Strings Without Consecutive Ones in Java

N, an integer, is provided. Our objective is to determine the overall number of strings with length N that do not include successive 1s. Example: Input: 3 Output: 6 Explanation The binary forms of length...

6 minutes read.

Java instance variable

An instance in object-oriented programming (OOP) is a particular implementation of any object. Each realized version of an item, which might vary in several ways, is an instance. Instantiation is...

3 minutes read.

Date time API in java

Introduction: In this text, we can talk approximately Data time API in java. The java.time, java.util, java.sql, and java.text packages contain classes that represent dates and times. The following classes are...

4 minutes read.

Java Coding Software

Desktop and web apps are created using Java, an object-oriented programming language. Java code may be executed on any platform, making it platform-independent. A text editor, tool, or piece of...

9 minutes read.

Java Constant

A constant is an unchangeable entity in coding, as its title implies. The value which cannot be altered, in other terms. We shall understand about Java constants and exactly how...

3 minutes read.

Brilliant Number in Java

It is a number N that is made up of two prime numbers that have the same number of digits and is called a brilliant number. Several/Some of the brilliant Numbers...

3 minutes read.

Snake and Ladder Problem in Java

Find the smallest number of dice throws necessary to reach the destination or last cell from the source or first cell on a snake and ladder board. Essentially, the player...

6 minutes read.

Construct the Largest Number from the Given Array in Java

In this section, we will create a Java programme that will enable you to locate the greatest integer in an array. The programme will begin comparing the array's numbers with...

3 minutes read.

New Features of Java 14

 Features like Switch expressions and text blocks which are previewed in Java 13 version are standardized in Java 14. Features in Java 14 Switch ExpressionText BlocksRecordsNull Pointer ExceptionsInstance ofPackaging ToolGarbage collectors 1.Switch Expressions Switch...

3 minutes read.

Stack Program in Java

Stack Program in Java The Stack class is part of the collection framework that inherits the Vector class. Thus, the Stack class can also be called a subclass of the Vector...

5 minutes read.

How to compare two dates in different format in Java?

We need to compare two dates frequently when coding. Real-world examples include sorting a list of persons by age or keeping track of students' attendance. To compare two dates, we...

7 minutes read.

How to download Eclipse for Java

Introduction: We write a java program, and when we want to run it, we need software to run it. Eclipse is a kind of software used to execute a JavaFX...

3 minutes read.

What is new in Java 17?

Java 17 LTS is the most recent long-term support release for the Java SE platform. Under the Oracle No-Fee Terms and Conditions License, which stands for long-term support, JDK 17...

6 minutes read.

Untouchable Number in Java

If a number N cannot be divided properly by any positive number, it is said to be an untouchable number. Additionally known as nonaliquot numbers. The sequence is A005114 from...

3 minutes read.

Java finalize()

Java finalize() In Java, the Object class is the root class that is inherited by all the Java classes. The class provides the finalize() method that is called just before the...

4 minutes read.

Alice and Bob Problem Java

Alice and Bob were two friends who liked to play games. Both together found a game, which has the description below. The game begins with an integer num, used to create...

2 minutes read.

Magnanimous Number in java

Magnanimous Number When the left and right halves of a majestic number are combined, the result is invariably a prime number, which must have at least two digits. The number's left...

3 minutes read.