scan的意思 JAVA_java语言中的scanner是什么意思用来干嘛

展开全部

scanner是一个用于扫描输入文本的新的实用程序。它是以前的StringTokenizer和Matcher类之间636f707962616964757a686964616f31333431373939的某种结合。

由于任何数据都必须通过同一模式的捕获组检索或通过使用一个索引来检索文本的各个部分。于是可以结合使用正则表达式和从输入流中检索特定类型数据项的方法。这样,除了能使用正则表达式之外,Scanner类还可以任意地对字符串和基本类型(如int和double)的数据进行分析。借助于Scanner,可以针对任何要处理的文本内容编写自定义的语法分析器。例如:

public class NextTest{

public static void main(String[] args) {

String s1,s2;

Scanner sc=new Scanner(System.in);

System.out.print("请输入第一个字符串:");

s1=sc.nextLine();

System.out.print("请输入第二个字符串:");

s2=sc.next();

System.out.println("输入的字符串是:"+s1+" "+s2);

}

}

运行结果是:

请输入第一个字符串:home

请输入第二个字符串:work

输入的字符串是:home work

6ec1f87d902c3521ee4de98bbb9e04b5.png

扩展资料:

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”还没有读取,并将cursor放在本行中)

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.(next()只读空格之前的数据,并且cursor指向本行)

next() 方法遇见第一个有效字符(非空格,非换行符)时,开始扫描,当遇见第一个分隔符或结束符(空格或换行符)时,结束扫描,获取扫描到的内容,即获得第一个扫描到的不含空格、换行符的单个字符串。

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.

nextLine()时,则可以扫描到一行内容并作为一个字符串而被获取到。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值