Java中Scanner类的用法

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

Scanner是SDK1.5新增的一个类,可是使用该类创建一个对象.
  
Scanner reader=new Scanner(System.in);
  
然后reader对象调用下列方法(函数),读取用户在命令行输入的各种数据类型:
  
next.Byte(),nextDouble(),nextFloat,nextInt(),nextLine(),nextLong(),nextShot()
  
上述方法执行时都会造成堵塞,等待用户在命令行输入数据回车确认.例如,拥护在键盘输入

12.34,hasNextFloat()的值是true,而hasNextInt()的值是false. NextLine()等待用户输入一个文

本行并且回车,该方法得到一个String类型的数据。

下面是一个实例:


import java.util.*;
public class Example{
public static void main(String args[]){
System.out.println("请输入若干个数,每输入一个数用回车确认");
System.out.println("最后输入一个非数字结束输入操作");
Scanner reader=new Scanner(System.in);
double sum=0;
int m=0;
while(reader.hasNextDouble()){
    double x=reader.nextDouble();
    m=m+1;
    sum=sum+x;
}
System.out.printf("%d个数的和为%f/n",m,sum);
System.out.printf("%d个数的平均值是%f/n",m,sum/m);
}
}
运行结果:
C:/java>java     Example请输入若干个数,每输入一个数用回车确认最后输入一个非数字结束输入操作34.13445d3个数的和为113.1000003个数的平均值是37.700000
C:/java>另一个例子,读取并分析文本文件:hrinfo.txt,文本文件的内容如下:老赵,28,feb-01,true小竹,22,dec-03,false阿波,21,dec-03,false凯子,25,dec-03,true   程序: import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class readhuman {
private static void readfile(String filename) {
try {
   Scanner scanner = new Scanner(new File(filename));
   scanner.useDelimiter(System.getProperty("line.separator"));
   while (scanner.hasNext()) {
      parseline(scanner.next());
   }
   scanner.close();
}catch (FileNotFoundException e) {
   System.out.println(e);
}
}
private static void parseline(String line) {
    Scanner linescanner = new Scanner(line);
    linescanner.useDelimiter(",");
    //可以修改usedelimiter参数以读取不同分隔符分隔的内容
    String name = linescanner.next();
    int age = linescanner.nextInt();
    String idate = linescanner.next();
    boolean iscertified = linescanner.nextBoolean();
    System.out.println("姓名:"+name+" ,年龄:"+ age+" ,入司时间:"+ idate+" ,验证标记:"+iscertified );
}
public static void main(String[] args) {
    if (args.length != 1) {
   System.err.println("usage: java readhuman file location");
   System.exit(0);
    }
   readfile(args[0]);
}
}
运行结果:C:/java>java     readhuman hrinfo.txt姓名:老赵 ,年龄:28 ,入司时间:feb-01 ,验证标记:true姓名:小竹,年龄:22 ,入司时间:dec-03 ,验证标记:false姓名:阿波 ,年龄:21 ,入司时间:dec-03 ,验证标记:false姓名:凯子,年龄:25 ,入司时间:dec-03 ,验证标记:true

 

import java.io.BufferedReader;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.util.Scanner;

public class Test {

    /**

    * @param args

    * @throws FileNotFoundException

    */

    public static void main(String[] args) throws FileNotFoundException {

        // TODO 自动生成方法存根

       

        // 申明一个Scanner 类的对象用来接受int 型的数据

        Scanner sc= new Scanner(System. in );

        System. out .print( " 请输入一个数字:" );

        int a=sc.nextInt();

        System. out .println( " 这个数字=" +a);

        //Scanner类的构造函数中的参数可以是BufferedReader的对象,进行快速读取文件

        sc= new Scanner( new BufferedReader( new FileReader( "d:/a.txt" )));

        String str;

       

        while (sc.hasNext())

        {

            System. out .println(sc.next());

           

        }

        String m= "Jin tian tian qi hao hao a ,wo meng qu chun you ba! " ;

       

        System. out .println(m);

       

        // 重新申明一个从键盘接受的Scanner 类的对象,用来接受字符串

        Scanner input = new Scanner(System. in );

        str = input.nextLine();

        System. out .println( " 此字符串为:" +str);

       

        input.close();

        sc.close();

       

    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值