温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

在Java中Scanner的用法总结

发布时间:2021-11-01 09:09:40 来源:亿速云 阅读:120 作者:小新 栏目:开发技术

这篇文章给大家分享的是有关在Java中Scanner的用法总结的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

Scanner类简介

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 work

把上面的程序修改一下:

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

感谢各位的阅读!关于“在Java中Scanner的用法总结”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI