×

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 String concat() method:

Java String concat() method is used to add the given String to the end of the current String. Syntax: public String concat(String str) Parameter: Str: String to be concatenated at the end of current...

1 minute read.

Heart Pattern in Java

Heart Pattern is yet another intricate pattern program, however, due to its complexity, interviewers hardly ever inquire about it. Method for Printing the Heart Number Pattern Put the value of the total row...

2 minutes read.

Java.sql.Time Format

In JDBC API as a cover aroundjava.util.Date that handles SQL-specific requirements we use the java.sql.Time. The java.sql.Time extends java.util.Date class. To represent SQL TIME, without a date this class is...

6 minutes read.

Thread Safety and How to Achieve it in Java

Before diving into the topic, let’s just recap the concept of Multithreading provided by Java where we can create and execute multiple threads of the same object. When these multiple...

5 minutes read.

CRC Program in Java

The acronym CRC stands for Cyclic Redundancy Check. It is invented by W. Wesley Peterson in 1961. It is an error detection technique that detects errors in digital networks (also...

5 minutes read.

Java Characters

Normally, when we work with characters, we use primitive data types char. When we have to work with the objects of char, we use Character class. Character class has many important...

2 minutes read.

Java Integer signum() method

The signum() method of Java Integer class returns the signum function of the specified int value. Syntax public static int signum (int i)  Parameters The parameter ‘i’ represents the value whose signum is to...

1 minute read.

3N+1 problem program in Java

The 3N+1 problem is a hypothesis in the field of abstract mathematics (not yet proven). Collectively known as the Collatz problem. This tutorial will describe about the 3N+1 problem and...

3 minutes read.

Figurate Number in Java

There have been several uses for figurate or figural numerals throughout history. A number that may be expressed by regular, distinct geometric shapes with spaced evenly points is referred to...

4 minutes read.

Graph Problems in Java

A graph is a special type of data structure used in programming that is made up of edges connecting each set of finitely many nodes, or vertices, to each other....

14 minutes read.

Find the Frequency of Each Element in the Array in Java

We may count the occurrence of each element in the array of items. Maintaining one array to store the counts of each array element is one strategy for solving this issue....

3 minutes read.

Program to find the duplicate characters in a string

Problem statement You have given with a string and your task is to find out the repeated characters from the string and print them. If no character is repeated, then you...

2 minutes read.

String Matches in Java

Firstly we have to know something about String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java...

3 minutes read.

Java TreeMap

TreeMap in Java with Example Java TreeMap implements the NavigableMap interface. It extends Map Interface. Java TreeMap is based on the red-black Tree implementation. It stores the key-value pair in sorted...

7 minutes read.

Java File Extension

A computer file's suffix is called its file extension. It is easily recognized since it appears immediately after a period (.) in the file name. Consider the file Demo.java as an...

3 minutes read.

Java Read File

Java provides its users with many ways of reading a file. To read a text file, you can use a FileReader, BufferedReader, or Scanner. Each utility offers something special for...

6 minutes read.

Java Long Keyword

Long is a primitive data type in Java. To initilize variables, we implement the long keyword. It can also be applied to techniques. A 64-bit two's complement integer can put...

3 minutes read.

How to initialize string array in Java

In this tutorial, we will learn how the string array is initialized in java. The initialization of string array in the java by using the NEW (it creates the object for...

3 minutes read.

Literals in Java

In this article we acknowledge ourselves about the term literals in java, types of literals and an example to each of them. Literal Literal refers to any constant value that can be...

4 minutes read.

Java 16

Java 16 is the most recent short-term incremental release, based on Java 15, and it was released on March 16, 2021. Records and sealed classes are just two of the...

11 minutes read.