Java中scanner类有next()和nextLine(),都是获取字符串,那它们有何区别呢?
next()读取的字符串没有空格,碰到空格就会停止读取。
nextLine()可以读取包含空格的字符串。
For example
public class demo01 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("请输入");
String str =in.nextLine();
System.out.println("输入的字符串为:"+str);
}
}
如果输入: hello world!
就会输出: hello world!
但是如果用next()
就会输出:hello