JavaSE---用户交互---获取键盘输入

1、概述

    1.1  JDK1.5提供了Scanner类,用来获取键盘输入

    1.2  Scanner类是   一个基于正则表达式文本扫描器,可以从文件、输入流、字符串中解析出基本类型值、字符串值;

    1.3  Scanner类提供了多个不同的构造器,可接受文件,输入流,字符串作为数据源,用于从文件、输入流、字符串中解析数据;

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
public final class Scanner implements Iterator<String>, Closeable {
 
    =====构造器
    private Scanner(Readable source, Pattern pattern) {
        assert source != null : "source should not be null";
        assert pattern != null : "pattern should not be null";
        this.source = source;
        delimPattern = pattern;
        buf = CharBuffer.allocate(BUFFER_SIZE);
        buf.limit(0);
        matcher = delimPattern.matcher(buf);
        matcher.useTransparentBounds(true);
        matcher.useAnchoringBounds(false);
        useLocale(Locale.getDefault(Locale.Category.FORMAT));
    }
    
    public Scanner(File source) throws FileNotFoundException {
        this((ReadableByteChannel)(new FileInputStream(source).getChannel()));
    }
 
    public Scanner(String source) {
        this(new StringReader(source), WHITESPACE_PATTERN);
    }
 
    public Scanner(InputStream source) {
        this(new InputStreamReader(source), WHITESPACE_PATTERN);
    }
}

    1.4  Scanner类提供的方法:

         hasNextXX():是否还有下一项;

         nextXX():获取下一项;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.util.Scanner;
 
public class TestScanner {
 
    public static void main(String[] args){
        boolean flag=true;
        while (flag){
            //System.in标准输入,即键盘输入
            Scanner scanner=new Scanner(System.in);
            String inputContent=scanner.next();
            if (inputContent.equals("exit")){
                flag=false;
            }
            System.out.println(inputContent);
        }
    }
}

        hasNextLine():是否有下一行;

        nextLine():获取下一行;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.util.Scanner;
 
public class TestScanner {
 
    public static void main(String[] args){
        boolean flag=true;
        while (flag){
            //System.in标准输入,即键盘输入
            Scanner scanner=new Scanner(System.in);
            String inputContent=scanner.nextLine();
            if (inputContent.contains("exit")){
                flag=false;
            }
            System.out.println(inputContent);
        }
    }
}

    1.5  Scanner类读取文件

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
import java.io.File;
import java.util.Scanner;
 
public class TestScanner {
    public static void main(String[] args)throws Exception{
        Scanner scanner=new Scanner(new File("D:/Work/mydemo/src/main/java/Test.java"));
        while (scanner.hasNextLine()){
            System.out.println(scanner.nextLine());
        }
    }
}
 
 
 
结果:
 
 
import java.util.Scanner;
 
public class Test {
 
    public static void main(String[] args){
        test();
    }
 
    public static void test(){
        String a="a";
        boolean flag=true;
        while (flag){
            Scanner scanner=new Scanner(System.in);
            String inputContent=scanner.next();
            if (inputContent.equals("exit")){
                flag=false;
            }
            System.out.println(a+":"+inputContent);
        }
    }
}

  

posted on   anpeiyong  阅读(398)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?

导航

< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示