Java中的 Scanner简述(企业笔试在线编程的输入控制)

摘要:

  最近进行企业在线笔试时,发现企业的笔试平台大都使用赛码网(虽然槽点很多),而且在线编程都需要使用Scanner来读取程序的输入,因此,笔者就在先辈们的成果上对Scanner做了一份全新的、详细的总结。我们知道,java.util.Scanner是Java5的新特征,主要功能是简化文本扫描。这个类最实用的地方表现在获取控制台输入,其他的功能都很鸡肋,尽管Java API文档中列举了大量的API方法,但是都不怎么地,特此简述。


一. 扫描控制台输入

  这个例子是常常会用到,但是如果没有Scanner,你写写就知道多难受了。在这个例子中,通过new Scanner(System.in)创建一个Scanner,控制台会一直等待输入,直到敲回车键结束,把所输入的内容传给Scanner,作为扫描对象。如果要获取输入的内容,则只需要调用Scanner的nextLine()方法即可。

/** 
* 扫描控制台输入 
* 
*/ 
public class TestScanner { 
        public static void main(String[] args) { 
                Scanner s = new Scanner(System.in); 
                System.out.println("请输入字符串:"); 
                while (true) { 
                        String line = s.nextLine(); 
                        if (line.equals("exit")) break; 
                        System.out.println(">>>" + line); 
                } 
        } 
}/* Output(): 
        请输入字符串: 
        234 
        >>>234 
        wer 
        >>>wer 
        bye
        >>>bye 
 *///
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

二. Scanner的便捷性

  如果说Scanner使用简便,不如说Scanner的构造器支持多种方式,构建Scanner的对象很方便。我们可以从字符串(Readable)、输入流、文件等等来直接构建Scanner对象,有了Scanner了,就可以 逐段(根据正则分隔式)来扫描整个文本,并对扫描后的结果做特定的处理。


三. Scanner分隔符

  Scanner 默认使用空格作为分隔符来分隔文本,但允许我们通过使用方法useDelimiter(String pattern)指定新的分隔符特别需要注意的是,方法useDelimiter(String pattern)的参数是一个正则表达式字符串,若想指定多个分隔符来分割,须使用”|”将它们隔开。如下所示:

public static void test7() {
    Scanner s = new Scanner("123 asdf sd 45 789 sdf asdfl,sdf.sdfl,asdf    ......asdfkl    las");
    s.useDelimiter(" |,|\\.");    // 将使用空格,逗号和点号来分割Scanner输入
    while (s.hasNext()) {
        System.out.println(s.next());
    }
}/* Output(): 
        123
        asdf
        sd
        45
        789
        sdf
        asdfl
        sdf
        sdfl
        asdf









        asdfkl



        las
 *///
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

四、Scanner常用API剖析和使用实例

  现列举Java中关于Scanner比较常用的几个API:

  • delimiter() 
      返回此 Scanner 当前正在用于匹配分隔符的 Pattern。

  • hasNext() 
      判断扫描器中当前扫描位置后是否还存在下一段,默认以一个或多个连续的空格作为段的分隔符。

  • hasNextLine() 
      如果在此扫描器的输入中存在另一行(以回车作为”分行符”),则返回 true。

  • next() 
      查找并返回来自此扫描器的下一个完整标记(取得输入段)。

  • nextLine() 
      此扫描器执行当前行,并返回跳过的输入信息(取得输入行文本)。


Scanner使用实例 (主要针对笔试编程中的多行输入和单行输入)

1). hasNextLine() 和 nextLine() 的用法

public class TestScanner1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // 正则表达式字符串
        //scanner.useDelimiter("\\*");

        System.out.println("请输入字符串:");

        // test1(scanner);
         test2(scanner);
        // test3(scanner);
    }


    /**
     * @description 以行(回车)读取,每在控制台输入三行则打印一次;不够三行,则阻塞等待
     * @author rico
     * @created 2017年4月5日 上午10:31:14
     * @param scanner
     */
    public static void test1(Scanner scanner) {
        while (true) {      // scanner 不断从控制台读取,若无内容,则阻塞等待

            // 以行读取
            String s1 = scanner.nextLine();
            String s2 = scanner.nextLine();
            String s3 = scanner.nextLine();

            System.out.println(">>>" + s1);
            System.out.println(">>>" + s2);
            System.out.println(">>>" + s3);
        }
    }


    /**
     * @description 以行(回车)读取,每在控制台输入三行则打印一次;不够三行,则阻塞等待(与方法test1等效)
     * @author rico
     * @created 2017年4月5日 上午10:31:14
     * @param scanner
     */
    public static void test2(Scanner scanner) {
        while (scanner.hasNextLine()) {        // 一旦控制台有输入,scanner就开始从控制台读取

            // 以行读取
            String s1 = scanner.nextLine();
            String s2 = scanner.nextLine();
            String s3 = scanner.nextLine();

            System.out.println(">>>" + s1);
            System.out.println(">>>" + s2);
            System.out.println(">>>" + s3);
        }
    }


    /**
     * @description 以行读取,每敲一次回车键读取一次,不会忽略空格
     * @author rico
     * @created 2017年4月5日 上午10:31:14
     * @param scanner
     */
    public static void test3(Scanner scanner) {
        while (scanner.hasNextLine()) {  // 一旦控制台有输入,scanner就开始从控制台读取
            String s1 = scanner.nextLine();
            System.out.println(">>>" + s1);
            System.out.println(">>>" + s1.length());
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70

             Scanner1.png-14.7kB


2). hasNext() 和 next() 的用法

public class TestScanner1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // 正则表达式字符串
        //scanner.useDelimiter("\\*");

        System.out.println("请输入字符串:");


        // test4(scanner);
        test5(scanner);
        //test6(scanner);
    }

    /**
     * @description 默认以一个或多个空格作为分割符来分隔行文本,每三个作为一组输出一次(不够三个则阻塞等待,直到够三个才作为一组进行输出);此外,连续输入三行(每行一个)也可以。
     * @author rico
     * @created 2017年4月5日 上午10:37:54
     * @param scanner
     */
    public static void test4(Scanner scanner) {
        while (true) {   // scanner 不断从控制台读取,若无内容,则阻塞等待
            String s1 = scanner.next();
            String s2 = scanner.next();
            String s3 = scanner.next();
            System.out.println(">>>" + s1);
            System.out.println(">>>" + s2);
            System.out.println(">>>" + s3);
        }
    }


    /**
     * @description 以一个或多个空格作为分割符来分隔行文本,每三个作为一组输出一次(不够三个则阻塞等待,直到够三个才作为一组进行输出);此外,连续输入三行(每行一个)也可以。
     * @author rico
     * @created 2017年4月5日 上午10:49:22
     * @param scanner
     */
    public static void test5(Scanner scanner) {
        while (scanner.hasNext()) {    // 一旦控制台有输入,scanner就开始从控制台读取
            String s1 = scanner.next();
            String s2 = scanner.next();
            String s3 = scanner.next();
            System.out.println(">>>" + s1);
            System.out.println(">>>" + s2);
            System.out.println(">>>" + s3);
        }
    }

    /**
     * @description 以一个或多个空格作为分割符来分隔行文本,以一个作为一组进行输出(所有的空格均为分割符),有几个就输出几个
     * @author rico
     * @created 2017年4月5日 上午10:37:54
     * @param scanner
     */
    public static void test6(Scanner scanner) {
        System.out.println(scanner.delimiter());
        while (true) {     // scanner 不断从控制台读取,若无内容,则阻塞等待
            String s1 = scanner.next();
            System.out.println(">>>" + s1);
            System.out.println(">>>" + s1.length());
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64

             scanner.png-42.7kB


3). nextLine() 与 next() 的区别

  • nextLine():以回车作为换行标志;

  • next() :以一个或多个空格作为分段标志,也可以以回车作为分段标志(如上图所示)。


五. Scanner 扫描文件输入

  看下面例子完成了使用Scanner 扫描文件输入:

 public static void main(String[] args) throws FileNotFoundException { 
    InputStream in = new FileInputStream(new File("C:\\AutoSubmit.java")); // 创建文件输入流
    Scanner s = new Scanner(in); 
    while(s.hasNextLine()){ 
        System.out.println(s.nextLine()); 
    } 
}/* Output(): 
        package own; 

        import java.io.BufferedReader; 
        import java.io.BufferedWriter; 
        import java.io.InputStream; 
        import java.io.InputStreamReader; 
        import java.io.OutputStreamWriter; 
        import java.net.HttpURLConnection; 
        import java.net.ProtocolException; 
        import java.net.URL; 

        import com.verisign.uuid.UUID; 

        /** 
        * @author wangpeng 
        * 
        */
        ...
 *///
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

引用:

java 中的Scanner(非常详细不看后悔)

猜你喜欢

转载自blog.csdn.net/liao1990/article/details/80822904