Reputation: 534
I've found a number of custom Scanner classes on the internet for fast Java IO, but none of them offer a custom hasNext() method implementation, so I don't know how to read data when input is of variable size.
Here's an example of one of these classes:
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() );
}
}
How could I go about writing this myself? Thanks.
Upvotes: 1
Views: 192
Reputation: 78
Unfortunately the BufferedReader doesn't have a hasNext()
function but it includes a ready()
function which is a boolean function that tells if there is an input to be read or not, You can find about it here https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html#ready-- .
So if you want to added to the class you mentioned it will go like this :
static boolean ready() throws IOException {return reader.ready();}
However take care if the input is not present ready()
will return false.
Upvotes: 2