[英]What is the advantage of using multiple scanners?
What is the advantage or use of using multiple scanner objects in a program? 在程序中使用多个扫描仪对象的优点或用途是什么?
Scanner sc = new Scanner(System.in);
as opposed to something like 而不是像
Scanner sc1 = new Scanner(System.in);
Scanner sc2 = new Scanner(System.in);
in the same program. 在同一个程序中。
I can see no advantage to the second bit of code: 我看到第二位代码没有任何优势:
Scanner sc1 = new Scanner(System.in);
Scanner sc2 = new Scanner(System.in);
and in fact there is some risk, risk if you close one Scanner (and thus the System.in
) before the other Scanner is done using it. 事实上,如果您在使用它之前关闭一个扫描仪(以及
System.in
)之前存在一些风险,则存在风险。
Instead, I can definitely see using more than one Scanner at times, but only one primary Scanner linking to System.in
. 相反,我肯定可以看到有时使用多个扫描仪,但只有一个主扫描仪链接到
System.in
。 Other Scanners can parse a line obtained. 其他扫描仪可以解析获得的线。 For example, use the main Scanner to get each line of text, and then use a second Scanner that has been fed the individual lines to parse the information held in the lines.
例如,使用主扫描仪获取每行文本,然后使用第二个扫描程序,该扫描程序已经输入各行以解析行中保存的信息。
eg, 例如,
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
String line = sc.nextLine();
Scanner lineScanner = new Scanner(line);
//.... parse line...
lineScanner.close();
}
sc.close();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.