問題描述
我的教授傾向于執行以下操作以從用戶那里獲取數字:
My professor tends to do the following to get a number from the user:
Scanner scanner = new Scanner(System.in);
Integer.parseInt(scanner.nextLine());
與簡單地做 scanner.nextInt()
相比有什么好處?
What are the benefits as opposed to simply doing scanner.nextInt()
?
java.util.Scanner.java
包含以下內容:
public int nextInt() {
return nextInt(defaultRadix);
}
public int nextInt(int radix) {
// Check cached result
if ((typeCache != null) && (typeCache instanceof Integer)
&& this.radix == radix) {
int val = ((Integer)typeCache).intValue();
useTypeCache();
return val;
}
setRadix(radix);
clearCaches();
// Search for next int
try {
String s = next(integerPattern());
if (matcher.group(SIMPLE_GROUP_INDEX) == null)
s = processIntegerToken(s);
return Integer.parseInt(s, radix);
} catch (NumberFormatException nfe) {
position = matcher.start(); // don't skip bad token
throw new InputMismatchException(nfe.getMessage());
}
}
在我看來,Scanner
本身也調用 Integer.parseInt() ,在額外的惡作劇之上.做簡單的 Integer.parseInt(scanner.nextLine())
有顯著的性能提升嗎?另一方面有什么缺點嗎?
As I see it, Scanner
calls Integer.parseInt() itself as well, on top of additional hocus pocus. Are there significant performance gains in doing simply Integer.parseInt(scanner.nextLine())
? Are there on the other hand any drawbacks?
掃描包含大量數據而不是用戶輸入的文件時會怎樣?
How about when scanning through a file with significant amount of data, and not a user input?
推薦答案
有2個觀察:
- 使用
myScannerInstance.nextInt()
會留下一個換行符.因此,如果您在nextInt()
之后調用nextLine()
,則nextLine()
將讀取換行符而不是實際數據.因此,您必須在nextInt()
之后添加另一個nextLine()
來吞噬那個 dangling 換行符.nextLine()
不會留下換行符.
- Using
myScannerInstance.nextInt()
leaves behind a new line character. So, if you callnextLine()
afternextInt()
, thenextLine()
will read the new line character instead of the actual data. Consequently, you will have to add anothernextLine()
after thenextInt()
to gobble up that dangling new-line character.nextLine()
doesn't leave behind a new line character.
代碼:
int age=myScannerInstance.nextInt();
String name = myScannerInstance.nextLine();// here the actual name will not be read. The new line character will be read.
nextInt()
將再次返回底層流并讀取.IO 調用需要時間(昂貴).它將進行大量檢查以獲取下一個整數.nextLine()
只會做一次這些檢查.因此,如果您調用一次nextLine()
并讀取 5 個整數(作為單行字符串),將它們拆分并解析為整數(使用Integer.parseInt()
),這將比單獨讀取每個 int 更快、更高效.
nextInt()
will again go back to the underlying stream and read. IO calls take time (expensive). It will do lot of checks to get the next integer.nextLine()
will do those checks only once. So, if you callnextLine()
once and read 5 integers (as a single line String), split them and parse them as integers (usingInteger.parseInt()
), it will be faster and more efficient than reading each int individually.
在運行非常大的循環時,使用 nextLine()
+ parseInt()
將為您帶來巨大的性能優勢.
Using nextLine()
+ parseInt()
will give you enormous performance benefit when you are running a very large loop.
用法:
使用 nextInt()
給你一個額外的好處,如果輸入文本不是整數,你會得到一個異常.示例 123
被接受.123sdsa
將拋出 InputMismatchException
.所以,你可以抓住它并適當地處理它.
Using nextInt()
gives you an additional advantage wherein you will get an exception if the input text is not an integer. example 123
is accepted.. 123sdsa
will throw an InputMismatchException
. So, you can catch it and handle it appropriately.
使用 nextLine()
將讀取整行,因此,它將讀取整個字符串 sada1231
然后如果它失敗并返回 NumberFormatException
無法將字符串解析為數字.您必須處理該異常.
Using nextLine()
will read the entire line, so, it will read the entire String sada1231
and then fail with NumberFormatException
if it cannot parse the String as a number. You will have to handle that exception.
通常,一個 nextLine()
/nextInt()
調用不會有太大的不同.如果你有一個循環或者你正在讀取大量數據,那么使用 readLine()
和 parseInt()
會非常有效.
Generally, one nextLine()
/ nextInt()
call won't make much of a difference. If you have a loop or if you are reading lot of data, then using readLine()
with parseInt()
will be very efficient.
這篇關于Integer.parseInt(scanner.nextLine()) 與scanner.nextInt()的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!