>>分享Java编程技术,对《Java面向对象编程》等书籍提供技术支持 书籍支持  卫琴直播  品书摘要  在线测试  资源下载  联系我们
发表一个新主题 开启一个新投票 回复文章 您是本文章第 31241 个阅读者 刷新本主题
 * 贴子主题:  Java API中的java.util.Scanner类用发总结 回复文章 点赞(0)  收藏  
作者:Jacky    发表时间:2018-11-27 22:00:06     消息  查看  搜索  好友  邮件  复制  引用

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

关于nextInt()、next()和nextLine()的理解

nextInt(): it only reads the int value, nextInt() places the cursor(光标) in the same line after reading the input.(nextInt()只读取数值,剩下”\n”还没有读取,并将cursor放在本行中)

next(): read the input only till the space. It can’t read two words separated by space. Also, next() places the cursor in the same line after reading the input.(next()只读空格之前的数据,并且cursor指向本行)
next() 方法遇见第一个有效字符(非空格,非换行符)时,开始扫描,当遇见第一个分隔符或结束符(空格或换行符)时,结束扫描,获取扫描到的内容,即获得第一个扫描到的不含空格、换行符的单个字符串。

nextLine(): reads input including space between the words (that is, it reads till the end of line \n). Once the input is read, nextLine() positions the cursor in the next line.
nextLine()时,则可以扫描到一行内容并作为一个字符串而被获取到。

public class NextTest{
   public static void main(String[] args) {
    String s1,s2;
    Scanner sc=new Scanner(System.in);
    System.out.print("请输入第一个字符串:");
    s1=sc.nextLine();
     System.out.print("请输入第二个字符串:");
    s2=sc.next();
    System.out.println("输入的字符串是:"+s1+" "+s2);
   }
}



结果:
请输入第一个字符串:home
请输入第二个字符串:work
输入的字符串是:home work1

把上面的程序修改一下:s1=sc.next(); s2=sc.nextLine();


运行结果:
请输入第一个字符串:home
请输入第二个字符串:
输入的字符串是:home


可以看到,nextLine()自动读取了被next()去掉的Enter作为他的结束符,所以没办法给s2从键盘输入值。经过验证,我发现其他的next的方法,如double nextDouble() , float nextFloat() , int nextInt() 等与nextLine()连用时都存在这个问题,解决的办法是:在每一个 next()、nextDouble() 、 nextFloat()、nextInt() 等语句之后加一个nextLine()语句,将被next()去掉的Enter结束符过滤掉。

public class NextTest{
  public static void main(String[] args) {
    String s1,s2;
    Scanner sc=new Scanner(System.in);
     System.out.print("请输入第一个字符串:");
     s1=sc.next(); sc.nextLine();
    System.out.print("请输入第二个字符串:");
    s2=sc.nextLine();
    System.out.println("输入的字符串是:"+s1+" "+s2);
  }
}



运行结果:
请输入第一个字符串:home
请输入第二个字符串:work
输入的字符串是:home work

循环输入多组测试用例

一个while就是一个测试用例

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

        // 一个while就是一个测试用例
        while(in.hasNext()){
            int n = in.nextInt(); // 该测试用例后续接收的参数个数
            long[] array = new long[n];
            String[] arrayStr = new String[n];
            for(int i=0; i<n; i++){
                arrayStr[i] = in.next();
            }
            for(int i=0; i<n; i++){
                array[i] = in.nextLong();// 取下一个元素转换成long类型
            }

            System.out.println(Arrays.toString(array)+" "+ Arrays.toString(arrayStr));
        }
    }



一个与容器结合的综合例子:

import java.util.Scanner;    
public class Main {    
    public static void main(String[] args) {    
        Scanner in = new Scanner(System.in);    
        while (in.hasNext()) {    
            int n = in.nextInt();  
        /* nextLine()是扫描器执行当前行,并返回跳过的输入信息,特别需要注意!!!

            如果没有该行,则执行第一个in.nextLine()命令时的返回值是int n = in.nextInt()的值*/  
            in.nextLine();  
        HashSet<String> set = new HashSet<String>();  
        for (int i = 0; i < n; i++) {  
        String line =  in.nextLine();  
        String[] arr = line.split(" ");  
        for (int j = 0; j < arr.length; j++) {  
            set.add(arr[j]);  
        }  
         }  
        System.out.println("sum:" + set.size());    

    }    
}



输入:
3
a b c
d e f
a b c

输出:
6


程序猿的技术大观园:www.javathinker.net
  Java面向对象编程-->输入与输出(上)
  JavaWeb开发-->数据类型
  JSP与Hibernate开发-->Servlet技术详解(Ⅱ)
  Java网络编程-->使用过滤器
  精通Spring-->使用JPA和注解
  Vue3开发-->Spring、JPA与Hibernate的整合
  类连接阶段的验证原理
  Java中保留数字的若干位小数位数的方法
  面试官问:如何排除Java虚拟机的GC引起的CPU飙高?
  购书咨询
  [讨论]书中多线程章节的语言表述有误?
  Java方法的嵌套与递归调用
  java NIO示例以及流程详解
  java常见的几种调用机制:同步调用,异步调用,回调
  Java入门实用代码: 集合中添加元素
  Java入门实用代码:遍历目录
  Java入门实用代码:打印平行四边形
  通过Java读取Excel数据
  Java判断一个字符是否为中文字符
  java使用gzip实现文件解压缩示例
  java Pattern和Matcher详解
  更多...
 IPIP: 已设置保密
楼主      
1页 0条记录 当前第1
发表一个新主题 开启一个新投票 回复文章


中文版权所有: JavaThinker技术网站 Copyright 2016-2026 沪ICP备16029593号-2
荟萃Java程序员智慧的结晶,分享交流Java前沿技术。  联系我们
如有技术文章涉及侵权,请与本站管理员联系。