Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.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中将输入字符串置于整数之后时出错_Java_String_Int_Java.util.scanner - Fatal编程技术网

在java中将输入字符串置于整数之后时出错

在java中将输入字符串置于整数之后时出错,java,string,int,java.util.scanner,Java,String,Int,Java.util.scanner,我试着这么做: int n = myScanner.nextInt(); for(int i=0;i<n;i++){ String str = myScanner.nextLine(); . . . } 然后它工作得很好。我想知道为什么会发生这种情况?传递整数时,需要使用输入的换行符: int n = myScanner.nextInt(); //gets only integers, no newline myScanner.nextLine(); //read

我试着这么做:

int n = myScanner.nextInt();
for(int i=0;i<n;i++){
   String str = myScanner.nextLine();
   .
   .
   .
}

然后它工作得很好。我想知道为什么会发生这种情况?

传递整数时,需要使用输入的换行符:

int n = myScanner.nextInt(); //gets only integers, no newline
myScanner.nextLine(); //reads the newline
String str;
for(int i=0;i<n;i++){
   str = myScanner.nextLine(); //reads the next input with newline
   .
   .
   .
}
int n=myScanner.nextInt()//只获取整数,不获取换行符
myScanner.nextLine()//读新行
字符串str;

对于(int i=0;i,需要使用传递整数时输入的换行符:

int n = myScanner.nextInt(); //gets only integers, no newline
myScanner.nextLine(); //reads the newline
String str;
for(int i=0;i<n;i++){
   str = myScanner.nextLine(); //reads the next input with newline
   .
   .
   .
}
int n=myScanner.nextInt();//只获取整数,不获取换行符
myScanner.nextLine();//读取换行符
字符串str;

对于(int i=0;i流中还有一个换行符。请使用@moffeltje的代码,或者尝试以下操作:

int n = Integer.parseInt(myScanner.nextLine());
for(int i=0;i<n;i++){
   String str = myScanner.nextLine();
   .
   .
   .
}
int n=Integer.parseInt(myScanner.nextLine());

对于(int i=0;i流中还有一个换行符。请使用@moffeltje的代码,或者尝试以下操作:

int n = Integer.parseInt(myScanner.nextLine());
for(int i=0;i<n;i++){
   String str = myScanner.nextLine();
   .
   .
   .
}
int n=Integer.parseInt(myScanner.nextLine());

对于(int i=0;我看一下解释。@Codebender我访问了上面的链接。在解决方案中,异常处理是在使用Integer.parseInt()时完成的,但当我使用parseInt时,它不会抛出任何异常。为什么会这样?@hermit,只有当无法解析时才会抛出
NumberFormatException
。但是
NumberFormatException
是未经检查的(扩展运行时异常)异常,因此您不必显式地编写或处理它(如果不这样做,它将抛出)。希望这是清楚的。@Codebender我认为尝试将换行符解析为整数值会引发异常。请看一看解释。@Codebender我访问了上面的链接。在解决方案中,异常处理是在使用integer.parseInt()时完成的,但当我使用parseInt时,它不会抛出任何异常。为什么会这样?@hermit,只有当无法解析时才会抛出
NumberFormatException
。但是
NumberFormatException
是未经检查的(扩展运行时异常)异常,因此您不必显式地编写或处理它(尽管如果你不这样做,它会被扔掉)。希望这是清楚的。@Codebender我认为尝试将换行符解析为整数值会引发异常。如果要使用此方法,则必须处理NumberFormatException,因为parseInt将尝试将换行符解析为整数value@hermit,
Scanner.nextLine()
将使用新行字符,但不会返回给调用者。因此,新行字符不会被解析。从Scanner java文档中,此方法返回当前行的其余部分,不包括末尾的任何行分隔符。位置设置为下一行的开头。您必须处理NumberFormatExcept如果要使用此方法,因为parseInt将尝试将换行符解析为整数,则返回value@hermit,
Scanner.nextLine()
将使用新行字符,但不会返回给调用者。因此,不会解析新行字符。从Scanner java文档中,此方法返回当前行的其余部分,不包括结尾处的任何行分隔符。位置设置为下一行的开头。