My professor tends to do the following to get a number from the user:
Scanner scanner = new Scanner(System.in);
Integer.parseInt(scanner.nextLine());
What are the benefits as opposed to simply doing scanner.nextInt()
?
java.util.Scanner.java
has the following in it:
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());
}
}
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?
nextLine() reads the remainder of the current line even if it is empty. nextInt() reads an integer but does not read the escape sequence "\n". next() reads the current line but does not read the "\n".
That's because the Scanner. nextInt method does not read the newline character in your input created by hitting "Enter," and so the call to Scanner. nextLine returns after reading that newline. You will encounter the similar behaviour when you use Scanner.
nextInt() method Scans the next token of the input as an int.An invocation of this method of the form nextInt() behaves in exactly the same way as the invocation nextInt(radix), where radix is the default radix of this scanner.
nextInt() The nextInt() method of a Scanner object reads in a string of digits (characters) and converts them into an int type. The Scanner object reads the characters one by one until it has collected those that are used for one integer. Then it converts them into a 32-bit numeric value.
Java Scanner nextInt() Method. The nextInt() method of Java Scanner class is used to scan the next token of the input as an int. There is two different types of Java nextInt() method which can be differentiated depending on its parameter.
Thats because the Scanner#nextInt method does not consume the last newline character of your input, and thus that newline is consumed in the next call to Scanner#nextLine Or, it would be even better, if you read the input through Scanner#nextLine and convert your input to integer using Integer#parseInt (String) method.
0 nextInt() reads a number, but doesn’t consume line separator. While nextLine() reads the String and consumes the new-line character. According to Java Docs: This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.
So, if you call nextLine()once and read 5 integers (as a single line String), split them and parse them as integers (using Integer.parseInt()), it will be faster and more efficient than reading each int individually. Using nextLine()+ parseInt()will give you enormous performance benefit when you are running a very large loop.
There are 2 observations :
myScannerInstance.nextInt()
leaves behind a new line character. So, if you call nextLine()
after nextInt()
, the nextLine()
will read the new line character instead of the actual data. Consequently, you will have to add another nextLine()
after the nextInt()
to gobble up that dangling new-line character. nextLine()
doesn't leave behind a new line character.code :
int age=myScannerInstance.nextInt();
String name = myScannerInstance.nextLine();// here the actual name will not be read. The new line character will be read.
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 call nextLine()
once and read 5 integers (as a single line String), split them and parse them as integers (using Integer.parseInt()
), it will be faster and more efficient than reading each int individually.Using nextLine()
+ parseInt()
will give you enormous performance benefit when you are running a very large loop.
Usage :
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With