我使用扫描器方法nextInt()和nextLine()读取输入。
它是这样的:
System.out.println("Enter numerical value");
int option;
option = input.nextInt(); // Read numerical value from input
System.out.println("Enter 1st string");
String string1 = input.nextLine(); // Read 1st string (this is skipped)
System.out.println("Enter 2nd string");
String string2 = input.nextLine(); // Read 2nd string (this appears right after reading numerical value)
问题是在输入数值之后,第一个input.nextLine()被跳过,第二个input.nextLine()被执行,因此我的输出看起来像这样:
Enter numerical value
3 // This is my input
Enter 1st string // The program is supposed to stop here and wait for my input, but is skipped
Enter 2nd string // ...and this line is executed and waits for my input
我测试了我的应用程序,看起来问题在于使用input.nextInt()。如果我删除它,那么string1 = input.nextLine()和string2 = input.nextLine()都按照我想要的方式执行。
那是因为扫描仪。nextInt方法不读取通过按“Enter”创建的输入中的换行符,因此调用Scanner。nextLine在读取换行符后返回。
当您使用Scanner时,您将遇到类似的行为。在Scanner.next()或任何Scanner之后的nextLine。nextFoo方法(除了nextLine本身)。
处理:
Either put a Scanner.nextLine call after each Scanner.nextInt or Scanner.nextFoo to consume rest of that line including newline
int option = input.nextInt();
input.nextLine(); // Consume newline left-over
String str1 = input.nextLine();
Or, even better, read the input through Scanner.nextLine and convert your input to the proper format you need. For example, you may convert to an integer using Integer.parseInt(String) method.
int option = 0;
try {
option = Integer.parseInt(input.nextLine());
} catch (NumberFormatException e) {
e.printStackTrace();
}
String str1 = input.nextLine();
如果你想快速扫描输入而不混淆到扫描器类nextLine()方法,请使用自定义输入扫描器。
代码:
class ScanReader {
/**
* @author Nikunj Khokhar
*/
private byte[] buf = new byte[4 * 1024];
private int index;
private BufferedInputStream in;
private int total;
public ScanReader(InputStream inputStream) {
in = new BufferedInputStream(inputStream);
}
private int scan() throws IOException {
if (index >= total) {
index = 0;
total = in.read(buf);
if (total <= 0) return -1;
}
return buf[index++];
}
public char scanChar(){
int c=scan();
while (isWhiteSpace(c))c=scan();
return (char)c;
}
public int scanInt() throws IOException {
int integer = 0;
int n = scan();
while (isWhiteSpace(n)) n = scan();
int neg = 1;
if (n == '-') {
neg = -1;
n = scan();
}
while (!isWhiteSpace(n)) {
if (n >= '0' && n <= '9') {
integer *= 10;
integer += n - '0';
n = scan();
}
}
return neg * integer;
}
public String scanString() throws IOException {
int c = scan();
while (isWhiteSpace(c)) c = scan();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = scan();
} while (!isWhiteSpace(c));
return res.toString();
}
private boolean isWhiteSpace(int n) {
if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true;
else return false;
}
public long scanLong() throws IOException {
long integer = 0;
int n = scan();
while (isWhiteSpace(n)) n = scan();
int neg = 1;
if (n == '-') {
neg = -1;
n = scan();
}
while (!isWhiteSpace(n)) {
if (n >= '0' && n <= '9') {
integer *= 10;
integer += n - '0';
n = scan();
}
}
return neg * integer;
}
public void scanLong(long[] A) throws IOException {
for (int i = 0; i < A.length; i++) A[i] = scanLong();
}
public void scanInt(int[] A) throws IOException {
for (int i = 0; i < A.length; i++) A[i] = scanInt();
}
public double scanDouble() throws IOException {
int c = scan();
while (isWhiteSpace(c)) c = scan();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = scan();
}
double res = 0;
while (!isWhiteSpace(c) && c != '.') {
if (c == 'e' || c == 'E') {
return res * Math.pow(10, scanInt());
}
res *= 10;
res += c - '0';
c = scan();
}
if (c == '.') {
c = scan();
double m = 1;
while (!isWhiteSpace(c)) {
if (c == 'e' || c == 'E') {
return res * Math.pow(10, scanInt());
}
m /= 10;
res += (c - '0') * m;
c = scan();
}
}
return res * sgn;
}
}
优点:
扫描输入比BufferReader更快
降低时间复杂度
为每个下一个输入刷新缓冲区
方法:
scanChar() -扫描单个字符
scanInt() - scan整数值
scanLong() -扫描长值
scanString() -扫描字符串值
scanouble () - scan双值
scanInt(int[] array) -扫描完整的数组(整数)
scanLong(long[] array) -扫描完整的array (long)
用法:
复制java代码下面的给定代码。
初始化给定类的对象
ScanReader sc = new ScanReader(System.in);
3.导入必要的类:
进口java.io.BufferedInputStream;
进口java.io.IOException;
进口java.io.InputStream;
4. 从主方法中抛出IOException来处理Exception
5. 使用提供的方法。
6. 享受
例子:
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
class Main{
public static void main(String... as) throws IOException{
ScanReader sc = new ScanReader(System.in);
int a=sc.scanInt();
System.out.println(a);
}
}
class ScanReader....
我想我来派对已经很晚了。
如前所述,在获取int值后调用input.nextLine()将解决您的问题。您的代码不能工作的原因是因为没有其他东西可以从您的输入(您输入int的地方)存储到string1。我将对整个主题进行更多的阐述。
将nextLine()视为Scanner类中nextFoo()方法中的一个特例。让我们举一个简单的例子。假设我们有如下两行代码:
int firstNumber = input.nextInt();
int secondNumber = input.nextInt();
如果我们输入下面的值(作为单行输入)
54 234
firstNumber和secondNumber变量的值分别变为54和234。这种工作方式的原因是因为当nextInt()方法接收值时,不会自动生成新的换行(即\n)。它只是接受“下一个int”并继续前进。除了nextLine(),其余的nextFoo()方法也是如此。
nextLine()在取值后立即生成新的换行;这就是@RohitJain说新的换行被“消耗”的意思。
最后,next()方法只接受最近的String而不生成新行;这使得它成为在同一行中获取单独字符串的优先方法。
我希望这能有所帮助。编码快乐!