java scanner

import java.util.Scanner;

Scanner scan=new Scanner(System.in);

//一定要读取到有效字符后才可以结束输入,自动去掉前面空格和第一个空格之后所有内容
// next() 不能得到带有空格的字符串
if(scan.hasNext()){
String str=scan.next(); //输入: hello world
System.out.println("str: " + str);//输出: hello
}

//以Enter为结束符,也就是说 nextLine()方法返回的是输入回车之前的所有字符
if(scan.hasNextLine()){
String str=scan.nextLine();
System.out.println("str: " + str);
}
// 常见还有hasNextInt/nextInt,hasNextDouble/nextDouble

scan.close();

原文地址:https://www.cnblogs.com/ShyPeanut/p/12691352.html