Java中Scanner类用法总结

Scanner类简介

Java 5添加了java.util.Scanner类,这是一个用于扫描输入文本的新的实用程序。它是以前的StringTokenizer和Matchaer类之间的某种结合。由于任何数据都必须通过同一模式的捕获组检索或通过使用一个索引来检索文本的各个部分。于是可以结合使用正则表达式和从输入流中检索特定类型数据项的方法。这样,除了能使用正则表达式外,Scanner类还可以任意地对字符串和基本类型的数据进行分析。借助于Scanner,可以针对任何要处理的文本内容编写自定义的语法分析器。

关于nextInt(),next(),nextLine()的理解

nextInt():it only reads the int value,nextInt() places the cursor(光标)in the same line after reading the input.(nextInt()只读取数字,剩下’\n’还没有读取,将crusor放在本行中)
next():read the input only till the space.It can’t read the two words separated by space.Also , next() places teh cursor in the same line after reading the input.(next()只读空格之前的数据,并且cursor指向本行)
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 nextLine().
nextLine()时,则可以扫描到一行内容并作为一个字符串而被获取到。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值