首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

Java中Scanner类应用详解

  • 25-03-07 07:41
  • 4232
  • 7191
blog.csdn.net

Java中的Scanner类应用详解

​ 在java编程中,Scanner类是一个用于读取数据的常用工具,可以从文件、输入流、字符串中读取数据。本文从常用构造方法、常用方法两个方面讲解其功能并举例说明。该类尚有其他的构造方法与一般方法,有技术开发需求的读者可以从官网查看API文档学习应用。

一、常用构造方法

1.Scanner(InputStream source)

​ 功能:构造一个新的 Scanner,它生成的值是从指定的输入流扫描的。开发者可根据实际需求使用Java IO中的输入流类创建对象作为参数传递给构造方法使用。

	//从文件读取数据,data.txt位于src目录,包含多个int型数值
	FileInputStream input = new FileInputStream("src/data.txt");
	Scanner scan = new Scanner(input);//用文件输入流作为数据源
	while(scan.hasNextInt())
		System.out.println(scan.nextInt());
  • 1
  • 2
  • 3
  • 4
  • 5
	 //从控制台输入数据
	Scanner scan = new Scanner(System.in);
    System.out.println("请输入年龄:"); 
    int age = scan.nextInt();
    System.out.println("你输入的年龄是:"+age);
  • 1
  • 2
  • 3
  • 4
  • 5

2.Scanner(File source)

​ 功能:构造一个新的 Scanner,它生成的值是从指定文件扫描的,以文件作为数据源。

     File fdata = new File("src/data.txt"); //data.txt包含数据123 456 789
     Scanner fscan = new Scanner(fdata); 
     while(fscan.hasNextInt()) {
     	 System.out.print(fscan.nextInt()+" "); //输出:123 456 789
     } 
     System.out.println();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

3.Scanner(String source)

​ 功能:构造一个新的 Scanner,它生成的值是从指定字符串扫描的。

	String str = "hello world!\nWelcome to China!";//两行内容
	Scanner scan = new Scanner(str);
	while(scan.hasNextLine()) {
		System.out.println(scan.nextLine());
	}
	/*
	输出两行内容:
	hello world!
	Welcome to China!
	*/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

4.Scanner(InputStream source, String charsetName)

​ 功能:构造一个新的 Scanner,它生成的值是从指定的输入流扫描的,且指定输入内容编码格式(字符集)。

	Scanner scan = new Scanner(System.in,"GBK");//指定编码格式GBK
	String str = scan.next();
	System.out.println(str);
  • 1
  • 2
  • 3

二、常用方法

​ 构造一个从输入流读取数据的对象scan,以下方法中若无特别指定,均使用该对象。

	Scanner  scan = new SCanner(Sytem.in);
  • 1

1.void close()

 关闭此扫描器。
  • 1
	scan.close();	//扫描器用完后关闭
  • 1

2.String next()

​ 查找并返回来自此扫描器的下一个完整标记。

	Scanner scan = new Scanner(System.in);
	String str = scan.next();//输入“hello”赋值给str
  • 1
  • 2
	String str = "20 a 30 b 40 c";
	Scanner scan = new Scanner(str);
	while(scan.hasNext()) {
		String s = scan.next();
		if(s.matches("\\d+"))
			System.out.println(s);
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

3.boolean hasNext()

​ 如果此扫描器的输入中有另一个标记,则返回 true。

	Scanner scanner = new Scanner(System.in);
    System.out.println("请输入以空格作为分隔符的字符串");//输入:hello world!
    while(scanner.hasNext()) 
        System.out.println(scanner.next());//分行输出hello、world!
  • 1
  • 2
  • 3
  • 4

4.String findInLine(Pattern pattern)

​ 试图在忽略分隔符的情况下查找下一个指定模式。

	String str3 = "1 fish 2";
	Scanner scan3 = new Scanner(str3);// 从str3中获取数据
	Pattern p = Pattern.compile("\\d+\\s*fish\\s*\\d+");
	scan3.findInLine(p);
	MatchResult result = scan3.match();	 	//返回此扫描器所执行的最后扫描操作的匹配结果。
	for(int i = 0; i <= result.groupCount(); i++)
		System.out.println(result.group(i));   //输出扫描结果
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

5.String findInLine(String pattern)

​ 试图在忽略分隔符的情况下查找下一个从指定字符串构造的模式。

	String str3 = "1 fish 2";
	Scanner scan3 = new Scanner(str3);// 从str3中获取数据
	scan3.findInLine("\\d+\\s*fish\\s*\\d+");
	MatchResult result = scan3.match();	  	//返回此扫描器所执行的最后扫描操作的匹配结果。
	for(int i = 0; i <= result.groupCount(); i++)
		System.out.println(result.group(i));   //输出扫描结果
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

6.boolean hasNextBigDecimal()

​ 如果通过使用 nextBigDecimal() 方法,此扫描器输入信息中的下一个标记可以解释为默认基数中的一个 BigDecimal,则返回 true。

	if(scan.hasNextBigDecimal())//接收并判断
			System.out.println(scan.nextBigDecimal());
  • 1
  • 2

7.boolean hasNextBigInteger()

​ 如果通过使用 nextBigInteger() 方法,此扫描器输入信息中的下一个标记可以解释为默认基数中的一个 BigInteger 值,则返回 true。

	if(scan.hasNextBigInteger())
			System.out.println(scan.nextBigInteger());
  • 1
  • 2

8.boolean hasNextBoolean()

​ 如果通过使用一个从字符串 “true|false” 创建的大小写敏感的模式,此扫描器输入信息中的下一个标记可以解释为一个布尔值,则返回 true。

	Scanner scan = new Scanner(System.in);
	System.out.println("请输入boolean型数据,以空格分割");
	while(scan.hasNextBoolean())
		System.out.println(scan.nextBoolean());
  • 1
  • 2
  • 3
  • 4

9.boolean hasNextByte()

​ 如果通过使用 nextByte() 方法,此扫描器输入信息中的下一个标记可以解释为默认基数中的一个字节值,则返回 true。

	 Scanner scanner = new Scanner(System.in);
     System.out.println("请输入以空格作为分隔符的byte类型数据");
     while(scanner.hasNextByte())
         System.out.println(scanner.nextByte());
  • 1
  • 2
  • 3
  • 4

10.boolean hasNextDouble()

​ 如果通过使用 nextDouble() 方法,此扫描器输入信息中的下一个标记可以解释为默认基数中的一个 double 值,则返回 true。

11.boolean hasNextFloat()

​ 如果通过使用 nextFloat() 方法,此扫描器输入信息中的下一个标记可以解释为默认基数中的一个 float 值,则返回 true。

12.boolean hasNextInt()

​ 如果通过使用 nextInt() 方法,此扫描器输入信息中的下一个标记可以解释为默认基数中的一个 int 值,则返回 true。

13.boolean hasNextLine()

​ 如果在此扫描器的输入中存在另一行,则返回 true。

		String str = "Hello world!\nWelcome to China!";
		Scanner scan = new Scanner(str);
		while(scan.hasNextLine())
			System.out.println(scan.nextLine());
  • 1
  • 2
  • 3
  • 4

14.boolean hasNextLong()

​ 如果通过使用 nextLong() 方法,此扫描器输入信息中的下一个标记可以解释为默认基数中的一个 long 值,则返回 true。

15.boolean hasNextShort()

​ 如果通过使用 nextShort() 方法,此扫描器输入信息中的下一个标记可以解释为默认基数中的一个 short 值,则返回 true。

16.MatchResult match()

​ 返回此扫描器所执行的最后扫描操作的匹配结果。

	String str3 = "1 fish 2 fish";
	Scanner scan3 = new Scanner(str3);// 从str3中获取数据
	scan3.findInLine("\\d+\\s*fish\\s*\\d+\\s*fish\\s*");
	MatchResult result = scan3.match();		
	for (int i = 1; i <= result.groupCount(); i++)
		 System.out.print(result.group(i) + " ");//输出匹配结果
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

17.BigDecimal nextBigDecimal()

​ 将输入信息的下一个标记扫描为一个 BigDecimal。

18.BigInteger nextBigInteger()

​ 将输入信息的下一个标记扫描为一个 BigInteger。

19.BigInteger nextBigInteger(int radix)

​ 将输入信息的下一个标记扫描为一个 BigInteger,radix是指定基数,可以是十进制(10)、二进制(0b10)、八进制(010)、十六进制(0x10)等格式。

	//10进制:10;二进制:0b10;八进制:010;十六进制:0x10
		BigInteger biginteger = scan.nextBigInteger(0b10);//接收二进制数据
		System.out.println(biginteger);//按十进制输出
  • 1
  • 2
  • 3

​ 在Scanner常用方法中,有多个方法以int radix作为参数,可参照本例指定符合需求的基数。

20.boolean nextBoolean()

​ 扫描解释为一个布尔值的输入标记并返回该值。

	boolean torf = scan.nextBoolean();
  • 1

21.byte nextByte()

​ 将输入信息的下一个标记扫描为一个 byte。

	byte bt = scan.nextByte();
  • 1

22.double nextDouble()

​ 将输入信息的下一个标记扫描为一个 double。

	double db = scan.nextDouble();
  • 1

23.float nextFloat()

​ 将输入信息的下一个标记扫描为一个 float。

	flaot ft = scan.nextFloat();
  • 1

24.int nextInt()

​ 将输入信息的下一个标记扫描为一个 int。

	int age = scan.nextInt()
  • 1

​ Scanner类的nextByte、nextShort、nextFloat、nextDouble、nextLong、nextBoolean、nextLine用法格

式同nextInt。

25.String nextLine()

​ 此扫描器执行当前行,并返回跳过的输入信息。

	String str = scan.nextLine()
  • 1

26.long nextLong()

​ 将输入信息的下一个标记扫描为一个 long。

	long number = scan.nextLong();
  • 1

27.short nextShort()

​ 将输入信息的下一个标记扫描为一个 short。

	short sr = nextShort();
  • 1

28.Scanner useDelimiter(Pattern pattern)

​ 将此扫描器的分隔模式设置为指定模式,在Scanner对象上设置一个分隔符,然后通过调用Scanner的

相关方法来获取按照该分隔符划分的输入内容。这样可以更方便地从 输入流只 中提取所需的数据。

 	 String input = "1 fish 2 fish red fish blue fish";
 	 //使用“fish”作为分隔符,\\s*:'\\'表示'\',\\s对应正则式中的\s,表示空白字符,*表示零或多个
 	 //从而:\\s*表示零个或多个空白符。\\s*fish\\s*表示fish前后有零或多个空白符
     Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");
     System.out.println(s.nextInt());  	//1
     System.out.println(s.nextInt());  	//2
     System.out.println(s.next());		//red
     System.out.println(s.next());		//blue
     s.close(); 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

29.Scanner useDelimiter(String pattern)

​ 将此扫描器的分隔模式设置为从指定 String 构造的模式。

	/* 
     使用转义字符"\n"作为分隔符,使得next()接收中间带空格的一行字符串,
     输入hello world后会出hello world,如果不使用"\n"作分隔符,只能接收到hello。
	*/
      Scanner ss = new  Scanner(System.in); 
      ss.useDelimiter("\n"); 
      System.out.println("请输入一行字符:");
      String str2 = ss.next(); 
      System.out.println(str2); 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

30.Scanner useRadix(int radix)

​ 将此扫描器的默认基数设置为指定基数。

	Scanner scan = new Scanner(System.in);
	scan.useRadix(0x10);
	int b = scan.nextInt();//也可以在该方法中指定基数:nextInt(0x10) 。输入:10
	System.out.println(b); //输出:16
		
  • 1
  • 2
  • 3
  • 4
  • 5

​ 本文介绍了Scanner类的常用构造方法和常用方法,在实际问题中,可以选择有效的构造方法创建扫描器对象,然后调用相关方法获取数值,从而解决实际问题。更多有用的知识和技能,可查看官网API。

注:本文转载自blog.csdn.net的海边漫步者的文章"https://blog.csdn.net/zbvcyzh/article/details/142299061"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

107
Java
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top