[JAVA] About the operation of next () and nextLine ()

About next () and nextLine () of the Scanner class

Make a note of what I thought about it.

behavior of next ()

next () searches for space-separated tokens and returns the tokens in the order they are found. It doesn't matter if you change it because it returns the token after capturing the input from the scanner once.

behavior of nextLine ()

nextLine () advances the current cursor to the beginning of the next line. Then, the skipped amount is output.

java


import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String str = sc.next();
		String line = sc.nextLine();

		System.out.println(str);
		System.out.println(line);

		sc.close();
	}
}

What happens if you enter something like the one below for the code above?

input


1
abcde

First, sc.next () returns the very first token. 1 is entered in str. Here the cursor is between 1 and the line break.

input


1[Line feed code]
 ^
here
abcde

Now, running sc.nextLine () will move the cursor to the beginning of the next line.

input


1[Line feed code]
 abcde
^
here

In other words, only the line feed code is read by nextLine ().

Recommended Posts

About the operation of next () and nextLine ()
About next () and nextLine () of the Scanner class
About the mechanism of the Web and HTTP
Think about the combination of Servlet and Ajax
About the handling of Null
About simple operation of Docker
[Grails] About the setting area and the setting items of application.yml
About the description of Docker-compose.yml
About the same and equivalent
[Technical memo] About the advantages and disadvantages of Ruby
I didn't understand the behavior of Java Scanner and .nextLine ().
[Ruby] Questions and verification about the number of method arguments
About the operation of Java reading party BOF-held monthly since 1998
[Ruby] About the difference between 2 dots and 3 dots of range object.
[Java] I thought about the merits and uses of "interface"
About the behavior of ruby Hash # ==
About the basics of Android development
About fastqc of Biocontainers and Java
About the equals () and hashcode () methods
This and that of the JDK
About the role of the initialize method
About removeAll and retainAll of ArrayList
Think about the 7 rules of Optional
Summary about the introduction of Device
About the log level of java.util.logging.Logger
About the version of Docker's Node.js image
What is testing? ・ About the importance of testing
Folding and unfolding the contents of the Recyclerview
About the initial display of Spring Framework
About the error message Invalid redeclaration of'***'
About the treatment of BigDecimal (with reflection)
About the difference between irb and pry
About the number of threads of Completable Future
Check the operation of the interface through threads
About the classification and concept of Immutable / Mutable / Const / Variable of Java and Kotlin.
[Ruby basics] About the role of true and break in the while statement
(Determine in 1 minute) About the proper use of empty ?, blank? And present?
Check the version of the JDK installed and the version of the JDK enabled
[Rails / ActiveRecord] About the difference between create and create!
About the official start guide of Spring Framework
About the description order of Java system properties
About the idea of anonymous classes in Java
Compare the speed of the for statement and the extended for statement.
Difference between next () and nextLine () in Java Scanner
[Java] The confusing part of String and StringBuilder
Chrome59 Comparison of normal and headless mode operation
I compared the characteristics of Java and .NET
About the method
Learn the rudimentary mechanism and usage of Gradle 4.4
About call timing and arguments of addToBackStack method
What are the advantages of DI and Thymeleaf?
A note about the Rails and Vue process
About the package
A memo about the types of Java O/R mappers and how to select them
Pass arguments to the method and receive the result of the operation as a return value
[Rough explanation] How to separate the operation of the production environment and the development environment with Rails
[Rails] I learned about the difference between resources and resources
Please note the division (division) of java kotlin Int and Int
[For beginners] DI ~ The basics of DI and DI in Spring ~
The comparison of enums is ==, and equals is good [Java]
About the usefulness of monads from an object-oriented perspective