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

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

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 next () and nextLine () of the Scanner class
About the operation of next () and nextLine ()
I didn't understand the behavior of Java Scanner and .nextLine ().
About the mechanism of the Web and HTTP
About the StringBuilder class
Think about the combination of Servlet and Ajax
Difference between next () and nextLine () in Java Scanner
[Grails] About the setting area and the setting items of application.yml
[Technical memo] About the advantages and disadvantages of Ruby
[Ruby] Class nesting, inheritance, and the basics of self
About the handling of Null
About the description of Docker-compose.yml
About the File :: Stat class
About the same and equivalent
[Ruby] Questions and verification about the number of method arguments
[Java] Set structure of collection class (about HashSet and TreeSet)
[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 the equals () and hashcode () methods
This and that of the JDK
Various methods of the String class
A murmur about the utility class
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
Read the first 4 bytes of the Java class file and output CAFEBABE
About the version of Docker's Node.js image
What is testing? ・ About the importance of testing
About the classification and concept of Immutable / Mutable / Const / Variable of Java and Kotlin.
Recorded because I was addicted to the standard input of the Scanner class
Folding and unfolding the contents of the Recyclerview
[Java] Inheritance and structure of HttpServlet class
[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?
About the initial display of Spring Framework
About the error message Invalid redeclaration of'***'
Proper use of interface and abstract class
About the treatment of BigDecimal (with reflection)
About the difference between irb and pry
About the number of threads of Completable Future
Display Japanese calendar and days of the week using java8 standard class
Get the class name and method name of Controller executed by HandlerInterceptor of Spring Boot
[Ruby] Difference between methods with and without self in the class. About class methods and instance methods.
I want to recursively get the superclass and interface of a certain class
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 description order of Java system properties
Compare the speed of the for statement and the extended for statement.
About date / time type of Doma2 entity class
[Java] The confusing part of String and StringBuilder
I compared the characteristics of Java and .NET
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
First touch of the Files class (or Java 8)
Class and model