Last active
January 14, 2021 18:06
-
-
Save lordvidex/0fe79ccaed2f06a60ad6927d3afa524e to your computer and use it in GitHub Desktop.
Fast Scanner class for reading values from terminal and files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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