When I mention "better", it means the program will reduce its use of memory, or the program will run faster.
In competetive coding, we seek better IO and reduce time and space complexity to access sample cases. I use Java specifically in competetive coding, and I wonder is there really a difference between using a input class or using java.io.BufferReader.
When I reviewed past USACO platinum problem solution written in Java, I saw uses of BufferedReader.
If I say I use BufferReader, the code will be:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class template {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
}
}
If I write a input class, the code will be (credit to: Department of Computer Engineering in Kasetsart University)
/** Class for buffered reading int and double values */
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
/** call this method to initialize reader for InputStream */
static void init(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input) );
tokenizer = new StringTokenizer("");
}
/** get next word */
static String next() throws IOException {
while ( ! tokenizer.hasMoreTokens() ) {
//TODO add check for eof if necessary
tokenizer = new StringTokenizer(
reader.readLine() );
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt( next() );
}
static double nextDouble() throws IOException {
return Double.parseDouble( next() );
}
}
I expected directly using BufferedReader could be better.