Skip to content

Instantly share code, notes, and snippets.

@lordvidex
Last active January 14, 2021 18:06
Show Gist options
  • Save lordvidex/0fe79ccaed2f06a60ad6927d3afa524e to your computer and use it in GitHub Desktop.
Save lordvidex/0fe79ccaed2f06a60ad6927d3afa524e to your computer and use it in GitHub Desktop.
Fast Scanner class for reading values from terminal and files
public class Test {
public static void main(String[] args) throws IOException {
...
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
//! Receives input from a file
public FastScanner(File file) throws FileNotFoundException {
br = new BufferedReader(new FileReader(file));
}
//! Receives input from the terminal
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
// @return - the next string token from the tokenizer
// if tokenizer `hasMoreElements` OR*
// create a new StringTokenizer that consumes a line from <BufferedReader/>
public String next() {
while(st == null || !st.hasMoreElements()){
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment