I understand what is Scanner good for, and also when to use Scanner and when BufferedReader.
I read a different, yet in some therm similar question Scanner vs. BufferedReader
Why is Scanner so slow when I read from the input?
I assume it has to do with that there is a small buffer in Scanner, but here I am lost.
The original problem is from, Codechef , but I am not interested in that solution.
Here is a code example with a given input:
Input:
- 7 3
- 1
- 51
- 966369
- 7
- 9
- 999996
- 1
And the code
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] s = br.readLine().split(" ");
int numberOfLines = Integer.parseInt(s[0]);
int divideNumber = Integer.parseInt(s[1]);
int count = 0;
for (int i = 0; i < numberOfLines; i++) {
String number = br.readLine();
if (number.length() < 11) {
int num = Integer.parseInt(number);
if (num % divideNumber == 0) {
count++;
}
}
}
System.out.println(count);
}
}
If I read the same code with scanner it is slow.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…