Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中使用nextLine获取多个输入_Java - Fatal编程技术网

如何在java中使用nextLine获取多个输入

如何在java中使用nextLine获取多个输入,java,Java,运行以下代码“线程中的异常”main“java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:0”时出错 我想知道next()和nextLine()方法之间的区别 //扫描仪 扫描仪sc=新的扫描仪(System.in); int n=sc.nextInt(); int arr[]=新的int[n]; 对于(int i=0;i我认为问题在于以下几行: if(str[i].charAt(0) == 'I' ){ 如果str为空字符串,则无法获取第一

运行以下代码“线程中的异常”main“java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:0”时出错 我想知道next()和nextLine()方法之间的区别

//扫描仪
扫描仪sc=新的扫描仪(System.in);
int n=sc.nextInt();
int arr[]=新的int[n];

对于(int i=0;i我认为问题在于以下几行:

if(str[i].charAt(0) == 'I' ){
如果
str
为空字符串,则无法获取第一个字符。可以使用
.isEmpty()
测试字符串是否为空

Scanner#nextLine()
读取
InputStream
(在您的例子中是
System.in
)中的下一行(如名称所示)

Scanner#next()
可以读取下一行,但也可以读取较少的内容。它会一直读取,直到到达特定的定界符


可以配置分隔符。默认情况下,它由分隔符分隔,如空格、断线等。

您可以共享异常发生的行吗?错误在哪行?
if(str[i].charAt(0) == 'I' ){