Home  >  Article  >  Java  >  What is the difference between next and nextLine methods in Java?

What is the difference between next and nextLine methods in Java?

王林
王林forward
2020-07-20 17:00:032610browse

What is the difference between next and nextLine methods in Java?

next() method will recognize spaces as the end of input.

(Recommended tutorial: java introductory tutorial)

Code example:

String str1 = sc.next();
System.out.println(str1);
输入 abcd efg
输出 abcd

nextLine() method will recognize a newline as the end of input.

(Video tutorial recommendation: java video tutorial)

Code example:

String str1 = sc.nextLine();
System.out.println(str1)

Summary:

requires spaces To truncate the input, use next(). For example, if you enter 1 (space) 2, you can get a=1, b=2;

If you need to include spaces, use nextLine(), for example, enter 1 (space) 2. , you can get a = 1 (space) 2.

The above is the detailed content of What is the difference between next and nextLine methods in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete