next()与nextLine()浅谈

      对于小白的我来说Scanner类下的这俩个方法让我着实的有点晕啊(其实还有很多让我晕的地方),所以我就通过看官方的文档和大神们的解释领悟到了些真谛,分享给大家。

首先我们先看一下官方文档:

  1. next()
    Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern.
    从这个扫描器找到并返回下一个完整的令牌。一个完整的令牌在前面和后面跟着与分隔符模式匹配的输入。

    简单说就是next()方法next()一定要读取到有效字符后才可以结束输入,对输入有效字符之前遇到的空格键、Tab键或Enter键等结束符,next()方法会自动将其去掉,只有在输入有效字符之后,next()方法才将其后输入的空格键、Tab键或Enter键等视为分隔符或结束符。简单地说,next()查找并返回来自此扫描器的下一个完整标记。完整标记的前后是与分隔模式匹配的输入信息,所以next方法不能得到带空格的字符串。
  2. nextLine()
    Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.
    将此扫描器向前推进,并返回被跳过的输入。该方法返回当前线路的其余部分,不包括末端的任何行分隔符。这个位置被设置为下一行的开始。
    Since this method continues to search through the input looking for a line separator, it may buffer all of the input searching for the line to skip if no line separators are present.
    由于这种方法继续在输入中寻找线分隔符,所以如果没有行分隔符,它可能会缓冲所有的输入搜索。

    意思是除了回车键别的都会被记录下来,并返回。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值