`
san_yun
  • 浏览: 2595091 次
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

文字扫描工具--java.util.Scanner

    博客分类:
  • java
 
阅读更多

A simple text scanner which can parse primitive types and strings using regular expressions.

A  Scanner   breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various  next   methods.

For example, this code allows a user to read a number from  System.in :

     Scanner sc = new Scanner(System.in);
     int i = sc.nextInt();
 

As another example, this code allows  long   types to be assigned from entries in a file  myNumbers :

      Scanner sc = new Scanner(new File("myNumbers"));
      while (sc.hasNextLong()) {
          long aLong = sc.nextLong();
      }

The scanner can also use delimiters other than whitespace. This example reads several items in from a string:

     String input = "1 fish 2 fish red fish blue fish";
     Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");
     System.out.println(s.nextInt());
     System.out.println(s.nextInt());
     System.out.println(s.next());
     System.out.println(s.next());
     s.close(); 

prints the following output:

     1
     2
     red
     blue 

The same output can be generated with this code, which uses a regular expression to parse all four tokens at once:

     String input = "1 fish 2 fish red fish blue fish";
     Scanner s = new Scanner(input);
     s.findInLine("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)");
     MatchResult result = s.match();
     for (int i=1; i<=result.groupCount(); i++)
         System.out.println(result.group(i));
     s.close(); 

The  default whitespace delimiter   used by a scanner is as recognized by  java.lang.Character .isWhitespace . Thereset   method will reset the value of the scanner's delimiter to the default whitespace delimiter regardless of whether it was previously changed.

A scanning operation may block waiting for input.

The  next   and  hasNext   methods and their primitive-type companion methods (such as  nextInt   and  hasNextInt ) first skip any input that matches the delimiter pattern, and then attempt to return the next token. Both  hasNext   and  next methods may block waiting for further input. Whether a  hasNext   method blocks has no connection to whether or not its associated  next   method will block.

分享到:
评论

相关推荐

    java输入语句scanner用法讲解.pdf

    Scanner是Java中的一个实用程序类,它位于java.util包下。Scanner类主要用于扫描输入文本,并可以从控制台、文件、输入流等读取原始数据,然后按照各种基本类型(如int、double、String等)的格式解析数据。 通过...

    Scanner类的用法

    Scanner类的用法-java.util.Scanner类,这是一个用于扫描输入文本的新的实用程序。它是以前的StringTokenizer和Matcher类之间的某种结合。由于任何数据都必须通过同一模式的捕获组检索或通过使用一个索引来检索文本...

    Java输入语句Scanner

    Java中的Scanner类是一个简单的文本扫描器,它可以从输入流中解析基本类型和字符串。要使用Scanner类,首先需要导入java.util包,然后创建一个Scanner对象,将System.in作为参数传递给Scanner的构造函数。接下来,...

    java类库源码-CSharp-Scanner:该存储库用于扫描仪实用程序的库代码,其功能与Java中的功能相似。Java中的Scanner类

    java.util命名空间中的Scanner类提供了帮助程序方法,以从指定的Source中读取基本类型。 该存储库的Scanner类尝试对泛型执行相同的操作。 支持所有.NET基本类型,因为它们仅需要实现“ IConvertible”接口。 在这里...

    -Desafio-2-Validar-linhas-de-c-digo-com-v-lidos:挑战N2 1°Bimestre

    导入java.util.Scanner; 主类{主要是空的静态受众(字符串[] args){尝试{系统。 出去。 println(“输入要验证的符号集(仅输入{(&lt;&gt;)}”);扫描仪读数=新的扫描仪(系统输入);字符串符号=读数。nextLine...

    TicTacToe_with_Java:使用Java的TicTacToe游戏

    ” 导入java.util.Scanner; 扫描仪扫描=新的扫描仪(System.in); 字符串str = scan.nextLine(); 与scan.nextLine()不同,scan.nextInt()不会占用新行。 因此,在scan.nextInt()之后,可能需要scan....

    kuhuesocket

    import java.util.Scanner; import serverSide.Goods; import socketFunction.ClientSocket; import systemUtil.Dates; import systemUtil.SysConstants; public class Client { private static final int ...

    Java Scaner类详解_动力节点Java学院整理

    Java.util.Scanner是Java5.0的新特征,主要功能是简化文本扫描。下面通过本文给大家分享java scaner类相关知识,需要的朋友下吧

    瓦列里·尼科洛夫(Valeri-Nikolov):equ1

    导入java.util.Scanner; 公共类调试{公共静态void main(String [] args){扫描仪扫描器=新Scanner(System.in); double test = 123.412423523523;// double celing = Math.ceil(test); //System.out.print...

    JavaCore

    Java核心第1天,从导入java.util.Scanner中扫描程序使用next(),nextLine(),nextDouble()

    FastJavaIO:速度非常快的Java输入阅读器

    InputReader提供了一种从输入流(如java.util.Scanner)中读取数据的方法,但速度要快多个数量级。 下面的图表概述了使用BufferedReader的InputReader段落之间的速度差异(Java扫描器太慢,无法在图表上显示)。 要...

    sqoop:为易于使用的 sqoop2 提供工人和扫描仪功能

    Scanner将通过扫描所需的源文件将原始类型的配置文件传输到即用型配置文件。 Util 现在,我们只支持csv格式的源文件: TABLE_NAME,PRIME_KEY_NAME 。 单击查看我们的示例csv文件。 Worker会将一个现成的配置文件...

Global site tag (gtag.js) - Google Analytics