Advertisement
Guest User

Untitled

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