Advertisement
Guest User

Untitled

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