Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.InputStreamReader;
  5. import java.util.InputMismatchException;
  6.  
  7. public class FastScanner {
  8. private BufferedReader br;
  9. private int pos, len;
  10. private char[] buffer = new char[4096];
  11. private boolean EOF = false;
  12. private StringBuilder sb = new StringBuilder();
  13.  
  14. public FastScanner (InputStream is) {
  15. br = new BufferedReader(new InputStreamReader(is));
  16. }
  17. // public FastScanner(String string)
  18. private void readBuffer() throws IOException{
  19. len = br.read(buffer);
  20. while (len == 0){
  21. len = br.read(buffer);
  22. }
  23. if (len == -1)
  24. {
  25. EOF = true;
  26. }
  27. pos = 0;
  28. }
  29.  
  30. public String readLine() throws IOException{
  31. StringBuilder sb = new StringBuilder();
  32. char c;
  33. while(hasNextChar()){
  34. c = nextChar();
  35. if (c == '\n')
  36. {
  37. break;
  38. }
  39. if (c != '\r') {
  40. sb.append(c);
  41. }
  42. }
  43. return sb.toString();
  44. }
  45.  
  46. public char nextChar() throws IOException {
  47. if (pos >= len){
  48. readBuffer();
  49. }
  50. return buffer[pos++];
  51. }
  52.  
  53. public int nextInt() throws IOException {
  54. skipBlank();
  55. sb = new StringBuilder();
  56. char c;
  57. while (hasNextChar()){
  58. c = nextChar();
  59. if (Character.isDigit(c) || c == '-')
  60. {
  61. sb.append(c);
  62. }
  63. else {
  64. break;
  65. }
  66. }
  67. try {
  68. return Integer.parseInt(sb.toString());
  69. } catch (NumberFormatException e) {
  70. throw new InputMismatchException();
  71. }
  72. }
  73.  
  74. public boolean hasNextChar() throws IOException{
  75. nextChar();
  76. pos--;
  77. return !EOF;
  78. }
  79.  
  80. public boolean hasNextInt() throws IOException{
  81. try {
  82. int next = nextInt();
  83. pos -= sb.toString().length();
  84. } catch (InputMismatchException e) {
  85. return false;
  86. }
  87. return true;
  88. }
  89.  
  90. private void skipBlank() throws IOException{
  91. while (true) {
  92. if (!hasNextChar() || !Character.isWhitespace(nextChar())) break;
  93. }
  94. pos--;
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement