Java 多次使用扫描仪
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26779393/
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
Java Using Scanner Multiple Times
提问by berkc
I am having this problem a lot. When I use a Scanner a lot of times, it doesn't get input from user.
我经常遇到这个问题。当我多次使用扫描仪时,它不会从用户那里获得输入。
Scanner scan = new Scanner(System.in);
System.out.println("1---");
int try1 = scan.nextInt();
System.out.println("2---");
int try2 = scan.nextInt();
System.out.println("3---");
String try3 = scan.nextLine();
System.out.println("4---");
String try4 = scan.nextLine();
When I run this code, result it :
当我运行此代码时,结果为:
1---
1---
12
12
2---
2---
321
321
3---
3---
4---
4---
aa
aa
As you can see, it skipped at 3rd input. Why this is happening? I solve this problem by using new Scanners, sometimes I have 5-6 different Scanners and it looks so complicated. Another problem is : there is an error "Resource leak: scan is never closed". I am really confused.
如您所见,它在第三个输入处跳过。为什么会这样?我通过使用新的扫描仪解决了这个问题,有时我有 5-6 个不同的扫描仪,看起来很复杂。另一个问题是:有一个错误“资源泄漏:扫描永远不会关闭”。我真的很困惑。
采纳答案by SMA
Use next API rather than nextLine as when you do nextInt you press enter and it generates number + \n and nextInt is only going to take an integer and won't take \n which in turn gets passed to next input i.e. try3 which is why it gets skipped.
使用 next API 而不是 nextLine,因为当您执行 nextInt 时,您按 Enter 并生成 number + \n 并且 nextInt 只会采用一个整数,而不会采用 \n 而后者又会传递给下一个输入,即 try3 这就是为什么它被跳过。
Scanner scan = new Scanner(System.in);
System.out.println("1---");
int try1 = scan.nextInt();
System.out.println("2---");
int try2 = scan.nextInt();
System.out.println("3---");
String try3 = scan.next();
System.out.println("4---");
String try4 = scan.next();
回答by Konstantin Yovkov
The problem is that by using scanner.nextInt()
you only read an integer value, but not the whole line and you don't consume the newline character (\n
) that is appended to the line when you press Enter.
问题是,通过使用scanner.nextInt()
您只能读取一个整数值,而不是整行,并且您不会使用\n
按 时附加到该行的换行符 ( ) Enter。
Then, when you process reading with scanner.nextLine()
you consume the newline character (\n
) and from the previous row and don't read the line you want to read. In order to force it to read it, you have to add an additional input.nextLine()
statement.
然后,当您处理阅读时,scanner.nextLine()
您会使用换行符 ( \n
) 和前一行,并且不要阅读您想要阅读的行。为了强制它读取它,您必须添加额外的input.nextLine()
语句。
System.out.println("2---");
int try2 = scan.nextInt();
System.out.println("3---");
scan.nextLine(); //<-- fake statement, to move the cursor on the next line
String try3 = scan.nextLine();
Not related to the question, you have to close the Scanner
, after finishing work, otherwise the compiler complains with a warning:
与问题无关,您必须Scanner
在完成工作后关闭, 否则编译器会发出警告:
scan.close();
回答by Gabriel Lidenor
Well, for the first question use
那么,对于第一个问题使用
scan.next()
扫描下一个()
insted of using
开始使用
scan.nextLine();
scan.nextLine();
For the second question I'd recommend using try, assuming you need to close your scan as your compiler is warning: "Resource leak: scan is never closed"
对于第二个问题,我建议使用 try,假设您需要关闭扫描,因为您的编译器发出警告:“资源泄漏:扫描从未关闭”
Scanner scanner= null;
try {
scanner= new Scanner(System.in);
}
finally {
if(scanner!=null)
scanner.close();
}