Java 不能同时使用 Scanner.nextInt() 和 Scanner.nextLine()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23036062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't use Scanner.nextInt() and Scanner.nextLine() together
提问by Umer Hassan
I have to get a string input and an integer input, but there order of input should be that integer comes first then user should be asked for string input
我必须得到一个字符串输入和一个整数输入,但输入的顺序应该是整数先出现然后用户应该被要求输入字符串
Scanner in = new Scanner(System.in);
input = in.nextLine();
k = in.nextInt();
in.close();
The above code works fine but if I take an integer input first like in the following code
上面的代码工作正常,但如果我像下面的代码一样首先输入一个整数
Scanner in = new Scanner(System.in);
k = in.nextInt();
input = in.nextLine();
in.close();
then it throws the java.lang.ArrayIndexOutOfBoundsException.
然后它抛出 java.lang.ArrayIndexOutOfBoundsException。
Here's the complete code of my source file:
这是我的源文件的完整代码:
import java.util.Scanner;
public class StringSwap {
公共类 StringSwap {
public static void main(String args[]) {
String input;
int k;
Scanner in = new Scanner(System.in);
k = in.nextInt();
input = in.nextLine();
in.close();
int noOfCh = noOfSwapCharacters(input);
originalString(input, noOfCh, k);
}
public static int noOfSwapCharacters(String s) {
char cS[] = s.toCharArray();
int i = 0, postCounter = 0;
while (cS[i] != '12345
hello
') {
if (cS[i] != '1 2 3 4 5 \n h e l l o \n
^
' && cS[i + 1] != '1 2 3 4 5 \n h e l l o \n
^
') {
cS[cS.length - 1 - postCounter] = '1 2 3 4 5 \n h e l l o \n
^
';
postCounter++;
}
i++;
}
return postCounter;
}
public static void originalString(String s, int noOfCh, int k) {
int counter = 1, chCounter = 0;
char cArray[] = s.toCharArray();
String post = "";
String pre = "";
String finalString = "";
char temp;
for (int i = 1; i <= k; i++) {
chCounter = 0;
counter = 1;
post = "";
pre = "";
for (int j = 0; j < cArray.length; j++) {
if (counter % 2 == 0 && chCounter <= noOfCh) {
temp = cArray[j];
post = temp + post;
cArray[j] = 'k = in.nextInt();
in.nextLine(); // Discard '\n'
input = in.nextLine();
';
chCounter++;
}
counter++;
}
for (int h = 0; h < cArray.length; h++) {
if (cArray[h] != '##代码##')
pre = pre + cArray[h];
}
finalString = pre + post;
for (int l = 0; l < finalString.length(); l++) {
cArray[l] = finalString.charAt(l);
}
}
System.out.println(finalString);
}
}
}
Kindly point out what I am doing wrong here.
请指出我在这里做错了什么。
采纳答案by dasblinkenlight
The problem is the '\n'
character that follows your integer. When you call nextInt
, the scanner reads the int
, but it does not consume the '\n'
character after it; nextLine
does that. That is why you get an empty line instead of the string that you were expecting to get.
问题'\n'
在于整数后面的字符。当您调用 时nextInt
,扫描器会读取int
,但不会消耗'\n'
其后的字符;nextLine
这样做。这就是为什么你得到一个空行而不是你期望得到的字符串。
Let's say your input has the following data:
假设您的输入具有以下数据:
##代码##Here is how the input buffer looks initially (^
represents the position at which the Scanner
reads the next piece of data):
这是输入缓冲区最初的样子(^
表示Scanner
读取下一条数据的位置):
After nextInt
, the buffer looks like this:
之后nextInt
,缓冲区看起来像这样:
The first nextLine
consumes the \n
, leaving your buffer like this:
第一个nextLine
消耗\n
,像这样离开你的缓冲区:
Now the nextLine
call will produce the expected result. Therefore, to fix your program, all you need is to add another call to nextLine
after nextInt
, and discard its result:
现在nextLine
调用将产生预期的结果。因此,要修复您的程序,您只需添加另一个对nextLine
after 的调用nextInt
,并丢弃其结果: