JAVA .next()和.nextLine()的区别

Scanner sc = new Scanner(System.in);
System.out.println("输入2个数字: ");
double a = sc.nextDouble();
double b = sc.nextDouble();
double c = 0;
System.out.println("输入运算符");
String operator1 = sc.next();
//String operator1 = sc.nextLine();
char operator1 = operator1.charAt(0);
用nextLine()时,一输完2个double数就会报错,在最后一行的代码出错
java.lang.StringIndexOutOfBoundsException: String index out of range
因为.charAt()而出错
而用next()取值时却一切运行正常,它们的区别是什么?
谢谢大家!

在java中,next()方法是不接收空格的,在接收到有效数据前,所有的空格或者tab键等输入被忽略,若有有效数据,则遇到这些键退出。
二nextLine()可以接收空格或者tab键,其输入应该以enter键结束。
当next()和nextLine()连用时,nextLine()会自动接收next()函数的结束符,所以为了避免数据接收有误,要避免二......
答案就在这里:java中next()和nextLine()的区别
----------------------你好,人类,我是来自CSDN星球的问答机器人小C,以上是依据我对问题的理解给出的答案,如果解决了你的问题,望采纳。

http://blog.csdn.net/zhouhx08/article/details/7531468