Java的Scanner类中next()方法与nextLine()方法的区别

来源:互联网 发布:三菱3u编程口接线图 编辑:程序博客网 时间:2024/05/29 17:51

在学习Java过程中,一次偶然机会,发现在Scanner类中,有两种接收控制台输入字符串的方法:next()和nextLine(),那么两者有什么不同之处呢?下面是我自己的一些总结。

一、JDK API文档中对这两种方法的定义:

     public String next() :   Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern.This method may block while waiting for input to scan, even if a previous invocation of Scanner.hasNext returned true.    

     public String nextLine():  Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.   

二、我的理解:

针对JDK文档的定义,我的理解是,对于next()方法,返回的是一个完整的扫描(即前后均有分隔符的有效字符串输入);而nextLine则重点在顺着输入的字符串寻找行分隔符回车键,再找到回车键之后,将之前跳过去的所有的输入均输出;

具体理解:

next():作为字符串输入的方法,自动屏蔽掉输入的分隔符,如空格、Tab键、回车键,直到遇到有效地字符输入后,则将这些分隔符作为结束标志;

nextLine();则是将从开始运行所输入的所有的字符,包括分隔符,均作为控制台输入,只有在遇到enter回车键时,才结束输入,并将所有的内容作为输入内容传给Scanner;

也就是说,next()方法并不能返回带空格、Tab键、回车符的字符串,而nextLine可以;如下面例子所述:

 

public class ScannerDemo{                           对于左边的例子,输出结果为:   public static void main(String []args{                      请输入字符串:    Scanner s=new Scanner(System.in);                               234    System.out.println("请输入字符串:");                             >>>234    while(true){                                                    23  45     //注:此处在输入23之后添加了几个空格然后输入45,得到的输出         String line=s.next();                                      >>>23           为将23看做一个输出,然后结束;再将45看做一个输出;         if(line.equals("exit")) break;                             >>>45         System.out.println(">>>"+line);}                           exit  }}

若将String line=s.next();  改为 String line=s.nextLine();  那么对于上述情况,输入  23    45 ,则会输出: >>>23   45  ;因为nextLine()方法只是将enter回车键作为分隔符,作为结束标志,而对于空格、tab键,均看做输入的字符串的一部分;这就是其与next()方法的区别;

三、两者连用的情况:

        对于将这两个方法用在同一个程序中,会有不一样的效果,如下面例子: 

  public class ScannerDemo{                           对于左边的例子,输出结果为:
      public static void main(String []args{                      请输入字符串:
            Scanner s=new Scanner(System.in);                               234
            System.out.println("请输入字符串:");                             456
                String line1=s.nextLine();                                           >>>234         //注:此处在输入234,然后回车输入45,然后回车得到的输出为分别对应两个方法
                String line2=s.next();                                                      >>>456         
                System.out.println(">>>"+line1);                                                                                    
                System.out.println(">>>"+line2);                                 

     }
}

但是,如果将程序中的两个方法交换一下位置,比如先写next()函数,再写nextLine()函数,那么在输入:234然后回车,那么输出的结果为:line1为“>>>”;line2为“>>>234”;这是因为,程序先执行next()函数,获得234字符串,并自动将回车键屏蔽掉,这样接下来执行nextLine()函数,默认将回车当做输入的字符,付给line1;这样就出现问题;


所以通常不建议将这两个方法连在一起用;



0 0
原创粉丝点击