C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » Java programming language

Java - Difference between next() and nextLine() methods

Here, we will learn about next() and nextLine() methods in java, what are the core concepts of next() and nextLine(), what are the differences between next() and nextLine() methods. Compare next() and nextLine() methods in Java.
Submitted by Preeti Jain, on January 31, 2018

1) next() method in java

  1. It is a method of Scanner class in java.
  2. next() method can read input till the space (i.e. it will print words till the space and whenever it gets space it stops working and give the result till the space).
  3. With the help of next() method we can't read those words which contain space itself (If we do then we will get irrelevant results).
  4. In other words next() method can take input till space and ends input of getting space.
  5. In next() method it places the cursor in the same line after reading the input.
  6. In next() its escaping sequence is space not ('\n').

Example: case 1 - Here, we will check how it works with space.

import java.util.Scanner;
class Input
{
	public static void main(String[] args){

	Scanner sc = new Scanner(System.in);
	System.out.println("Enter you Skills");
	String skills = sc.next();
	System.out.println("your skills are " + skills);
	}
}

Output

D:\Java Articles>java Input
Enter you Skills
c c++ java
your skills are c

Example: case 2 - Here, we will check how it works without space.

import java.util.Scanner;
class InputWithoutSpace
{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter you First Name");
		String fn = sc.next();
		System.out.println("your First Name is " + fn);
	}
}

Output

D:\Java Articles>java InputWithoutSpace
Enter you First Name
Preeti
your First Name is Preeti

2) nextLine() method in java

  1. It is a method of Scanner class in java.
  2. nextLine() method can read input till the line change (i.e. it will print words till the line change or press enter or '\n' and whenever it gets '\n' or press enter it stops working and give the result of whole line until press enter or line change).
  3. With the help of nextLine() method Also we can read those words which contain spaces itself .
  4. In other words nextLine() method can take input till the line change or new line and ends input of getting '\n' or press enter.
  5. In nextLine() method it places the cursor in the new or next line after reading the input.
  6. In nextLine() its escaping sequence is '\n' or press enter not space.

Example: case 1 - Here, we will check how it works with space.

import java.util.Scanner;
class NextLine
{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter your name");
		String name = sc.nextLine();
		System.out.println("your name is " + name);
	}
}

Output

D:\Java Articles>java NextLine
Enter your name
Preeti Jain
your name is Preeti Jain


Liked this article? Do share with your friends :)


COMMENTS