IT干货网

Scanner类

xmjava 2022年03月12日 编程设计 182 0

Scanner类用以获取用户输入的信息,用户输入后,可使用next()和nextLine()进行接收。

使用next()

package com.cxf.scanner; 
 
import java.util.Scanner; 
 
public class Demo2 { 
    public static void main(String[] args) { 
        Scanner scanner = new Scanner(System.in); 
 
        System.out.println("使用next方式接收:"); 
        String str1 = scanner.next(); 
 
        System.out.println("next接收的内容为:"); 
        System.out.println(str1); 
 
        scanner.close(); 
    } 
} 
 

输出及输出结果:

使用next方式接收: 
hello world 
next接收的内容为: 
hello 

输入了hello world ,输出却没有world,这是因为next()把空格视为终止。

使用nextLine()

package com.cxf.scanner; 
 
import java.util.Scanner; 
 
public class Demo3 { 
    public static void main(String[] args) { 
        Scanner scanner = new Scanner(System.in); 
 
        System.out.println("使用nextLine方式接收:"); 
        String str2 = scanner.nextLine(); 
 
        System.out.println("nextLine接收的内容为:"); 
        System.out.println(str2); 
 
        scanner.close(); 
    } 
} 
 

输入及输出结果:

使用nextLine方式接收: 
hello world 
nextLine接收的内容为: 
hello world 

与next()不同,nextLine()不把空格视为终止。

额外发现

起初试图把上述两个实验在同一个类里做掉:

package com.cxf.scanner; 
 
import java.util.Scanner; 
 
public class Demo1 { 
    public static void main(String[] args) { 
        Scanner scanner = new Scanner(System.in); 
 
        System.out.println("使用next方式接收:"); 
        String str1 = scanner.next(); 
 
        System.out.println("使用nextLine方式接收:"); 
        String str2 = scanner.nextLine(); 
 
        System.out.println("next接收的内容为:"); 
        System.out.println(str1); 
 
        System.out.println("nextLine接收的内容为:"); 
        System.out.println(str2); 
 
        scanner.close(); 
    } 
} 
 

输入及输出结果:

使用next方式接收: 
hello world 
使用nextLine方式接收: 
next接收的内容为: 
hello 
nextLine接收的内容为: 
 world 

这是因为第10行next()接收了hello,从后面的空格开始就停止接收。

而从空格开始的这部分内容” world“并没有丢失。

第13行执行nextLine(),我预期他会再让我输入一次,但是没有,因为他识别并接收到了" world"。

那如果我只输入hello呢?按照道理nextLine()就识别不到新的信息,这样的话他会让我再输入一次吗?结果如下:

使用next方式接收: 
hello 
使用nextLine方式接收: 
next接收的内容为: 
hello 
nextLine接收的内容为: 
 

nextLine()接收到的内容为空。

因此我们只能输入一次,next()和nextLine()共用了这次输入。

如果从scanner入手呢?

package com.cxf.scanner; 
 
import java.util.Scanner; 
 
public class Demo1 { 
    public static void main(String[] args) { 
        Scanner scanner1 = new Scanner(System.in); 
        Scanner scanner2 = new Scanner(System.in); 
 
        System.out.println("scanner1接收:"); 
        String str1 = scanner1.next(); 
 
        System.out.println("scanner2接收:"); 
        String str2 = scanner2.nextLine(); 
 
        System.out.println("scanner1接收的内容为:"); 
        System.out.println(str1); 
 
        System.out.println("scanner2接收的内容为:"); 
        System.out.println(str2); 
 
        scanner1.close(); 
        scanner2.close(); 
    } 
} 
 

输入及输出结果:

scanner1接收: 
hello world 
scanner2接收: 
hey you 
scanner1接收的内容为: 
hello 
scanner2接收的内容为: 
hey you 

之前的代码中next()和nextLine()都属于同一个Scanner。

这次代码里让他们分属两个Scanner,分别为scanner1和scanner2,这时候他让我们进行了两次键盘输入。

因此可以推断,每个Scanner的第一个next或nextLine方法运行时,要求一次键盘输入。之后同一个Scanner再执行next或nextLine方法时,不会再要求输入,而是从第一次输入的内容里,剔除掉之前next方法接收了的部分,把剩下的部分作为这次next方法的输入。


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!