Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is nextLine() in Java?
3.
Syntax of nextLine() in Java
4.
Parameter of nextLine() in Java
5.
Return Values of nextLine() in Java
6.
Example of nextLine() in Java
6.1.
Example 1
6.2.
Java
7.
difference between next() and nextLine() in Java?
8.
Advantages and disadvantages
9.
Frequently asked questions
9.1.
What is the function of nextLine() method in scanner class?
9.2.
Give an example of the exception the function nextLine() in Java can throw.
9.3.
Give examples of more methods of Scanner class other than next() and nextLine() in Java.
9.4.
What will be the output if next() and nextLine() in Java are used, and the input is "Hello Ninjas"?
10.
Conclusion
Last Updated: Mar 27, 2024
Easy

nextLine() in Java

Author Komal
0 upvote
Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

Introduction

Welcome, Ninjas! This blog will follow a series of topics like next() and nextLine() in Java, but before that, we will discuss the basics of Java language to familiarize you with its concepts.

nextLine() in Java

Java is a high-level language developed by James Gosling and was released in 1995 to the world. One of the best features is that it is an object-oriented language and is class-based. It can be used on various platforms and open source. It is a general-purpose language used to compile and run code anywhere. Source files are saved with the extension .java. A java program, after being written, is first compiled into bytecode which produces a file with a .class extension. It is used for mobile applications, desktop applications, and web applications.

There are various classes and packages in Java. One of them is the Scanner Class present in Java.util package. It is used for taking the input of data types like int, double, etc., and strings. The scanner class consists of next() and nextLine() methods. 

Let us get started! 

Must Read, Multithreading in java, Duck Number in Java

What is nextLine() in Java?

Similar to next(), we have the method nextLine() in Java.

nextLine() in Java is also a method from the Scanner class and is used to take string inputs from the user. So, to use this method nextLine() in Java, firstly, we create an object of the Scanner class. This method takes the input until a new line is encountered, i.e., enter is not entered.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Syntax of nextLine() in Java

For declaring, use the following syntax to read the input of the String type:

public String nextLine()

 

To implement, first import the necessary Scanner class as follows:

import java.util.Scanner

 

Then, you have to first create a scanner class object like the one below:

Scanner s = new Scanner(System.in)

 

It is created to read from the standard input(System.in).

Then, you have to call the nextLine() method to take the following line of the input by the user.

String inp = s.nextLine()

 

This is the whole syntax for implementing the nextLine() method in Java.

Parameter of nextLine() in Java

The nextLine() method in Java does not accept any parameter.

Return Values of nextLine() in Java

The nextLine() method in Java's Scanner class reads and returns the entire current input line, including any newline characters or spaces.

Example of nextLine() in Java

Example 1

  • Java

Java

import java.util.Scanner;
public class ninjas{
   public static void main(String args[]){
   //creating object of Scanner class
   Scanner sc=new Scanner(System.in);
   //Taking input by nextLine()
   System.out.print("Enter the string you want to print: ");
   String ninja=sc.nextLine();
   //printing output
   System.out.println(ninja);
   }
}
output

difference between next() and nextLine() in Java?

In this section of the blog, we will see the difference between the two methods, next() and nextLine(), in Java.

next() in Java

nextLine() in Java

next() in Java is a method of Scanner class. nextLine() in Java is also a method of Scanner class.
next() in Java takes input until space is encountered nextLine() in Java takes input until enter is entered.
The cursor is placed in the same line after taking input. The cursor is placed in the next line after taking input.
The syntax is sc.next(), where sc is the object of the scanner class. The syntax is sc.nextLine(), where sc is the object of Scanner class.

Advantages and disadvantages

Both functions - next() and nextLine() in Java form a part of the Scanner class and thus help take input in the form of a string. 

The disadvantage of the next() method is that it takes the input only until a space is not encountered; similarly, the nextLine() in Java method takes input until we do not press enter to the next line.

Must Read: Java System Out Println and Hashcode Method in Java

Frequently asked questions

What is the function of nextLine() method in scanner class?

The nextLine() method in Java's Scanner class reads a line of text from the input until a newline character or the end of the input, returning it as a string.

Give an example of the exception the function nextLine() in Java can throw.

One of the exceptions is NoSuchElementException which is thrown when no line is found in the input.

Give examples of more methods of Scanner class other than next() and nextLine() in Java.

Some examples of methods of the Scanner class are - nextInt() which is used to read input value from the user, and close(), which is used to close the Scanner object.

What will be the output if next() and nextLine() in Java are used, and the input is "Hello Ninjas"?

The output will be "Hello" if next() is used, and it will be “Hello Ninjas” if nexLine() in Java is used.

Conclusion

We hope this blog was easy to understand. Firstly, We discussed the basic concepts of java language and its features, followed by the use of next() and nextLine() in Java and their implementation. This blog contains the example of next () in Java and nextLine() in Java.

If you found this blog interesting and insightful, refer to similar blogs:

Python Introduction

Python Operators

Refer to the Basics of C++ with Data StructureDBMS, and Operating System by Coding Ninjas, and keep practicing on our platform Coding Ninjas Studio. You can check out the mock test series on code studio.

You can also refer to our Guided Path on Coding Ninjas Studio to upskill yourself in domains like Data Structures and AlgorithmsCompetitive ProgrammingAptitude, and many more! Refer to the interview bundle if you want to prepare for placement interviews. Check out interview experiences to understand various companies' interview questions.

Give your career an edge over others by considering our premium courses!

Happy Learning!

Thankyou image

 

Previous article
File Input/Output
Next article
File Input Stream class
Live masterclass