scanner next和scanner nextLine的区别

该问题源于浏览StackOverflow时遇到的一个提问

问题链接

源代码:
public class Factorials {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        char c = 'Y';
        int num = 0;

        do
         {
            System.out.print("Enter a number to calculate its factorial: ");
            while (!sc.hasNextInt()) { 
                System.out.println("Invalid Entry - Enter only Integers! Try Again: ");
                sc.nextLine();
            }
            
            int result = 1;
            num = sc.nextInt();
            for(int i = 1; i <= num; i++) {
                result = result * i;
            }
            System.out.println("The factorial of " + num + " is: " + result);
            
            System.out.println("Do you wish to continue? Y/N: ");
            
            c = sc.next().charAt(0);
        }while(c == 'y' || c == 'Y');
        sc.close();
    }       
}
程序功能:

读取控制台输入,如果是数字,则进行某种计算。然后根据输入是否为Y/y,决定后面的计算。如果输入的是非数字,那么需要提醒输入数字。

问题:

当输入过数字,比如1之后,继续输入Y,让程序继续跑。此时如果输入一个非数字比如a,会发现程序第12行,也就是

System.out.println("Invalid Entry - Enter only Integers! Try Again: ");

错误信息输出了两遍。

解析:

这个问题关键的点就是scanner.next和scanner.nextLine问题,关于这个两个方法,可以直接看一下jdk的描述。

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. This method may block while waiting for input to scan, even if a previous invocation of hasNext returned true.

也就是next方法会读取一个完整的token,会忽略前后的分隔符。

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.

该方法会读取完整的一行,但会排除行分隔符。

所以问题出现在当输入Y后,其实后面是有一个回车的,next方法读取到Y后,会结束。但在第11行判断的时候,第一个判断到的还是回车。第二个判断的才是输入的非数字a。该结论可以通过将第13行修改为以下方式进行验证。

String str = sc.nextLine();
System.out.println();
解决:

方法1:在第25行后,显示调用nextLine方法,将剩余的字符直接吞掉。

方法2:将循环判断内的nextLine方法,替换为next方法,忽略分隔符。

通过一番探索之后,也补充到了该问题的回答中。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

51iwowo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值