Scanner类

Scanner是一个新的操作类,是在java.util包中提供的一个操作类,使用此类可以方便的完成输入流的输入操作。

import java.io.*;
import java.util.Scanner;
public class ScannerDemo {
 public static void main(String args[]) throws IOException
 {
  //通过Scanner接收输入流
  Scanner scan=new Scanner(System.in);
  //输入整形
  int i=0;
     if(scan.hasNextInt())
     {
      i=scan.nextInt();
     }
     //输入字符串
     String str=null;
     if(scan.hasNext())
     {
      str=scan.next();
     }
     //输入的时候进行正则匹配
     if(scan.hasNext("\\d"))
     {
      str=scan.next();
     }
     //从文件中读取
     File file=new File("D:"+File.separator+"demo.txt");
        Scanner scan2=new Scanner(new FileInputStream(file));
     StringBuffer sbf=new StringBuffer();
        scan.useDelimiter("\n");
     while(scan.hasNext())
     {
      sbf.append(scan.next()).append('\n'); 
     }
        System.out.println(i+"  "+str);
 }
}

Java 5添加了java.util.Scanner类,这是一个用于扫描输入文本的新的实用程序。它是以前的StringTokenizer和Matcher类之间的某种结合。由于任何数据都必须通过同一模式的捕获组检索或通过使用一个索引来检索文本的各个部分。于是可以结合使用正则表达式和从输入流中检索特定类型数据项的方法。这样,除了能使用正则表达式之外,Scanner类还可以任意地对字符串和基本类型(如int和double)的数据进行分析。借助于Scanner,可以针对任何要处理的文本内容编写自定义的语法分析器。
Scanner是SDK1.5新增的一个类,可是使用该类创建一个对象.
  
Scanner reader=new Scanner(System.in);
  
然后reader对象调用下列方法(函数),读取用户在命令行输入的各种数据类型:
  
nextByte(),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

原文地址:https://www.cnblogs.com/jinzhengquan/p/1948393.html