Advertisement
biznesman

Untitled

Jul 24th, 2021
1,072
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.33 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.io.*;
  4.  
  5. public class modScanner {
  6.     private Reader reader;
  7.     private char lastChar;
  8.     private StringBuilder string = new StringBuilder("");
  9.     private StringBuilder integer = new StringBuilder("");
  10.     private boolean used = false;
  11.  
  12.     public modScanner(Reader r) {
  13.         reader = r;
  14.     }
  15.  
  16. //    public modScanner(InputStream in) {
  17. //        this(new BufferedReader(new InputStreamReader(in)));
  18. //    }
  19.  
  20.     public modScanner(File file) throws IOException {
  21.         this(new BufferedReader(new FileReader(file)));
  22.     }
  23.  
  24.     public void close() throws IOException {
  25.         reader.close();
  26.     }
  27.  
  28.     private boolean nextSymbolReader() throws IOException {
  29.         int ch = reader.read();
  30.         if (ch >= 0) {
  31.             lastChar = (char) ch;
  32.             return true;
  33.         }
  34.         return false;
  35.     }
  36.  
  37.     private boolean createString() throws IOException {
  38.         if (!used) {
  39.             string.delete(0, string.length());
  40.             while (nextSymbolReader() && !Character.isWhitespace(lastChar)) {
  41.                 string.append(lastChar);
  42.             }
  43.             return string.length() > 0;
  44.         }
  45.         return string.length() > 0;
  46.     }
  47.  
  48.     private boolean createStringInt() throws IOException {
  49.         if (!used) {
  50.             integer.delete(0, integer.length());
  51.             while (nextSymbolReader() && !Character.isWhitespace(lastChar) && (Character.isDigit(lastChar) || lastChar == '.')) {
  52.                 integer.append(lastChar);
  53.             }
  54.             if (integer.length() > 0 && integer.charAt(integer.length() - 1) == '.'){
  55.                 createStringInt();
  56.             }
  57.             return integer.length() > 0;
  58.         }
  59.         return integer.length() > 0;
  60.     }
  61.  
  62.     public boolean hasNext() throws IOException {
  63.         if (used) {
  64.             used = false;
  65.             return createString();
  66.         }
  67.         return createString();
  68.     }
  69.  
  70.     public boolean hasNextInt() throws IOException {
  71.         if (used) {
  72.             used = false;
  73.             return createStringInt();
  74.         }
  75.         return createStringInt();
  76.     }
  77.  
  78.  
  79.     public String next() throws IOException {
  80.         used = true;
  81.         return string.toString();
  82.     }
  83.  
  84.     public int nextInt() throws IOException {
  85.         return 0;
  86.     }
  87.  
  88. }
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement