老程序员分享:nextInt和nextLine以及next方法的区别

简介: 老程序员分享:nextInt和nextLine以及next方法的区别

"

最近//代码效果参考:https://v.youku.com/v_show/id_XNjQwNjU4OTc1Mg==.html

在刷算法题的时候,发现如下问题

1 Scanner in = new Scanner(System.in)

2

3 int n = in.nextInt();

4

5 String str = in.nextLine();

在控制台中,输入:

3

hello

发现str的值为空,说明nextLine方法,没有读取到""hello""字符串。

为了解决以上问题,现将控制台输入内容的读取方法总结下。

一、nextInt()

it only reads the int value, nextInt() places the cursor in the same line after reading the input.

  只读取整形的数据,输入读取完之后,将光标放在同一行。

  换句话说,当我们使用该方法时,会将光标放在读取数字后面,并且是同一行。

二、nextLine()

reads input including space between the words (that is, it reads till the end of line \n). Once the input is read, nextLine() positions the cursor in the next line.

  读取空格,直到以'\n'为标志的行尾。一旦输入读取完毕,该方法会将光标移到下一行开始的位置。

三、next()

read the input only till the space. It can't read two words separated by space. Also, next() places the cursor in the same line after reading the input.

  读取输入内容直到遇到空格。它不能读取两个字符之间的空格。同时将光标放在读取输入后面,并且在同一行。

四、实验

  1、实验一

1 public class Test {

2 public static void main(String【】 args) {

3 Scanner in = new Scanner(System.in);

4 int n = in.nextInt();

5 while(n>0){

6 String test = in.nextLine();

7 System.out.println(""test is :""+test );

8 n--;

9 }

10 }

11 }

  查看输入输出:

  解释:

  输入:""2回车""

  输出:""test is :""

  第一步:输入2回车后,in.nextInt()读取整形数字2,并将光标移到""2""和""回车""之间

  第二步:接下来,in.nextLine()将读取”回车""内容,因为在控制台中不能显示,所以其内容为空

  第三步:输入hello后,in.nextLine()方法读取hello方法

  输入:""2 a b回车""

  输出:""test is : a b""

  输入:""hello回车""

  输出:""test is :hello""

  解释:

  第一步:in.nextInt()方法将光标移动到""2""和“空格a空格b回车”之间;

  第二步:in.nextLine()方法读取光标之后的内容,即“空格a空格b回车”

  第三步:输入""hello回车"",in.nextLine()方法读取""hello回车内容"",并输出""this is:hello""

  2、实验二

1 public class Test {

2 public static void main(String【】 args) {

3//代码效果参考:https://v.youku.com/v_show/id_XNjQwMDEzMzQ4OA==.html

Scanner in = new Scanner(System.in);

4 int n = in.nextInt();

5 while(n>0){

6 String test = in.next();

7 System.out.println(""test is :""+test);

8 n--;

9 }

10

11 }

12 }

  解释:

  第一步:in.nextInt将读入的数字初始化给n,现在光标在""2""和""空格a空格b""之间

  第二步:in.next()方法读取光标之后的""空格a"",因为next()不能读取空格,所以遇到第二个空格就停止读取,第一个空格被忽略。此时光标在第二个空格之前

  第三步:读入b


"
image.png
相关文章
|
2月前
|
存储 XML 缓存
Java字符串内幕:String、StringBuffer和StringBuilder的奥秘
Java字符串内幕:String、StringBuffer和StringBuilder的奥秘
35 0
|
2月前
|
存储 算法 安全
【数据结构与算法初学者指南】【冲击蓝桥篇】String与StringBuilder的区别和用法
【数据结构与算法初学者指南】【冲击蓝桥篇】String与StringBuilder的区别和用法
|
11月前
|
安全 Java
灵魂拷问:你真的理解System.out.println()打印原理吗?
灵魂拷问:你真的理解System.out.println()打印原理吗?
96 0
|
存储 安全 算法
《我要进大厂》- Java集合夺命连环13问,你能坚持到第几问?(Map | Collections)
《我要进大厂》- Java集合夺命连环13问,你能坚持到第几问?(Map | Collections)
《我要进大厂》- Java集合夺命连环13问,你能坚持到第几问?(Map | Collections)
next 与 nextLine 方法的区别
next 与 nextLine 方法的区别
305 0
next 与 nextLine 方法的区别
|
Java
java学习第十天笔记-字符串198-stringbuilder提高效率图
java学习第十天笔记-字符串198-stringbuilder提高效率图
92 0
java学习第十天笔记-字符串198-stringbuilder提高效率图
Scanner 使用 、注意要点
Scanner 使用 、注意要点
191 0
|
存储 SQL 缓存
震惊,99.9% 的同学没有真正理解字符串的不可变性
稍有些基础的同学都知道 Java 中 String 字符串是“不可变”的,想要使用“可变字符串”可以使用 `StringBuilder` 和 `StringBuffer` 。 大多数讲字符串不可变性的文章大同小异。但实际上大多数人的理解并不对。
152 0
震惊,99.9% 的同学没有真正理解字符串的不可变性
|
缓存 Java Linux
System.currentTimeMillis() 和 System.nanoTime() 哪个更快?别用错了!
System.currentTimeMillis() 和 System.nanoTime() 哪个更快?别用错了!
176 0
|
API 索引
在forEach面前大多数人都可能会犯的错
使用过`forEach`的人大致有两种:普通使用,简简单单;复杂使用,总想搞出点花样来,结果一些莫名其妙的bug就出现了,解决这些bug所花费的时间都可以换一种思路实现了,能用作`for`循环的,又不只是`forEach`。没错,笔者就是后者,终究是自己“学艺不精”。于是乎,花点时间,结合自己的实际开发经验,再来好好理理`forEach`。
159 0
在forEach面前大多数人都可能会犯的错

热门文章

最新文章