Advertisement
Guest User

idk if this is even java anymore

a guest
Mar 22nd, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.04 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.util.InputMismatchException;
  4. import java.util.regex.Pattern;
  5.  
  6. public class NumericBufferInput {
  7.     //In order to use this function, pass a buffer, a String to print to the screen, and one of the lexer lambda expressions
  8.     //within this class
  9.     public static String applyLexerToBufferInput(BufferedReader ioStream, String printMe, LexString lexer) throws IOException {
  10.         String abomination = null;
  11.         boolean brrrrrramp;
  12.         do {
  13.             brrrrrramp = false;
  14.             try {
  15.                 System.out.println(printMe);
  16.                 abomination = ioStream.readLine();
  17.                 lexer.lex(abomination);
  18.             } catch (InputMismatchException e) {
  19.                 brrrrrramp = true;
  20.             }
  21.         } while (brrrrrramp);
  22.         return abomination;
  23.     }
  24.  
  25.     @FunctionalInterface
  26.     public interface LexString {
  27.         void lex(String s) throws InputMismatchException;
  28.     }
  29.  
  30.     //determines if a string will parse as an Int/Long Literal that is non-octal
  31.     public static final LexString isNaturalNumber = (String inputString) -> {
  32.         var numberMatch = Pattern.compile("^(0|[1-9]\\d*)$").matcher(inputString);
  33.         if (!numberMatch.find()) {
  34.             throw new java.util.InputMismatchException();
  35.         }
  36.     };
  37.  
  38.     //determines if a string is Double/Float literal that is non-octal
  39.     public static final LexString isFractional = (String amIFractional) -> {
  40.         var fracMatch = Pattern.compile("^(0|\\d+?)\\.\\d+$").matcher(amIFractional);
  41.         if (!fracMatch.find()) {
  42.             throw new java.util.InputMismatchException();
  43.         }
  44.     };
  45.  
  46.     //determines if a string is either a Double/Float literal, or an Int/Long literal
  47.     public static final LexString isWholeOrFractional = (String possibleNumber) -> {
  48.         try {
  49.             isFractional.lex(possibleNumber);
  50.         } catch (java.util.InputMismatchException e) {
  51.             isNaturalNumber.lex(possibleNumber);
  52.         }
  53.     };
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement