Class MathScanner

java.lang.Object
com.github.gbenroscience.parser.MathScanner

public class MathScanner extends Object
Author:
GBEMIRO
  • Field Details

  • Constructor Details

    • MathScanner

      public MathScanner(String scannerInput)
      Parameters:
      scannerInput - the input of this Scanner object
  • Method Details

    • setScannerInput

      public void setScannerInput(String scannerInput)
      Parameters:
      scannerInput - sets the input of this Scanner object
    • getScannerInput

      public String getScannerInput()
      Returns:
      the input into the scanner.
    • setScanner

      public void setScanner(ArrayList<String> scanner)
      Parameters:
      scanner - sets the ArrayList object that holds the scanned output.
    • getScanner

      public List<String> getScanner()
      Returns:
      the ArrayList object that holds the scanned output.
    • setRunnable

      public void setRunnable(boolean runnable)
      Parameters:
      runnable - sets whether object of this class are runnable on a calculating device or not.
    • isRunnable

      public boolean isRunnable()
      Returns:
      true if the object of this class is runnable on a calculating device.
    • getFirstNumberSubstring

      public static String getFirstNumberSubstring(String val) throws StringIndexOutOfBoundsException
      method getFirstNumberSubstring takes a String object and returns the first number string in the String.
      Parameters:
      val - the string to analyze
      Returns:
      the index of the first occurrence of a number character in the sequence.
      Throws:
      StringIndexOutOfBoundsException
    • splitStringAtFirstNumber

      public List<String> splitStringAtFirstNumber(String val)
      Parameters:
      val - the String to analyze
      Returns:
      an ArrayList consisting of 2 or 3 parts. a. If a number substring starts the string,then it returns an ArrayList object containing the number substring and the part of the string after the number. e.g. for 231.62A+98B+sin45C, the method returns [231.62,A+98B+sin45C] b. If a number substring does not start the string but occurs later on in it, the method returns an ArrayList object containing (i.)the substring from index 0 up to, but not including the index where the first number occurs. (ii.)The number substring (iii.)The remaining part of the string after the number. The method will not be used directly by developers but will be used as a tool in math expression scanning.
    • splitStringOnNumbers

      public List<String> splitStringOnNumbers(String val)
      method splitStringOnNumbers takes a String object and returns an ArrayList of substrings in which the original string has been split into different parts based on the amount of number substrings it has.
      Parameters:
      val - the string to analyze e.g if val="123.234+43.6",the output will be [123.234,+,43.6]
      Returns:
      An ArrayList of substrings consisting of number substrings and the other substrings of the input.
      Throws:
      StringIndexOutOfBoundsException
    • getNumberStrings

      public ArrayList<String> getNumberStrings(String val)
      method getNumberStrings takes a String object and returns an ArrayList of substrings of all numbers in the input String
      Parameters:
      val - the string to analyze e.g if val="123.234+43.6",the output will be [123.234,43.6]
      Returns:
      An ArrayList of substrings consisting of number substrings and the other substrings of the input.
      Throws:
      StringIndexOutOfBoundsException
    • splitStringOnMethods_Variables_And_Operators

      public void splitStringOnMethods_Variables_And_Operators()
      Split the scannerInput String on the operators.
    • validateInputAfterSplitOnMethodsAndOps

      public void validateInputAfterSplitOnMethodsAndOps()
      Check that variables and methods do not conflict..if that can happen. Now method names and variable names are similar. The only difference between them is that methods use parentheses while variables don't. Enforce especially then that variable-parenthesis combinations that may be mistaken for methods are not allowed. e.g sin(..) is a method. A user may name a variable sin, if he/she so wishes. make sure that the user does not do things like.. varname(expr..). Rather, he/she must separate variables from parentheses using the multiplication operator.
    • validateTokens

      public void validateTokens()
      Identifies that the input is a valid one by checking that all tokens are either Number objects, Variable objects or Operator objects. Then it registers any Variable object found and initializes it to zero.
    • plusAndMinusStringHandlerHelper1

      public static final List<String> plusAndMinusStringHandlerHelper1(List<String> scanner)
      Handles unary plus and minus operations on the numbers that come after them Also handles repeated concatenations of plus and minus operators.
    • plusAndMinusStringHandlerHelper

      public static final List<String> plusAndMinusStringHandlerHelper(List<String> scanner)
    • scanner

      public List<String> scanner()
      Utility method,more popularly used as a scanner into mathematical tokens of mathematical expressions. This method does not check expression variables against declared variables and is only useful in breaking down the input into scanned tokens of numbers,variables and operators. The overloaded one takes a VariableManager object that models the store of Variable objects in a computing application and checks if all variable names entered by the user have been declared.
      Returns:
      an ArrayList containing a properly scanned version of the string such that all recognized number substrings,variable substrings and operator substrings have been resolved. To use this method as a scanner for math expressions all the programmer has to do is to take care of the signs where need be(e.g in -2.873*5R+sinh34.2, the scanned view will be [-,2.873,*,5,R,+,sinh,34.2]) To make sense of this, the programmer has to error minor code to concatenate the - and the 2.873 and so on.
    • scanner

      public List<String> scanner(VariableManager varMan)
      Utility method,more popularly used as a scanner into mathematical tokens of mathematical expressions
      Parameters:
      varMan - The store of all user variables already declared. The scanner will not allow the user to use any variables he/she/it has not declared
      Returns:
      an ArrayList containing a properly scanned version of the string such that all recognized number substrings,variable substrings and operator substrings have been resolved. To use this method as a scanner for math expressions all the programmer has to do is to take care of the signs where need be(e.g in -2.873*5R+sinh34.2, the scanned view will be [-,2.873,*,5,R,+,sinh,34.2]) To make sense of this, the programmer has to error minor code to concatenate the - and the 2.873 and so on.
    • recognizeAnonymousFunctions1

      public static void recognizeAnonymousFunctions1(List<String> scanner)
    • recognizeAnonymousFunctions

      public static void recognizeAnonymousFunctions(List<String> scanner)
      Optimized invalid input: '&' more correct 2025 version of recognizeAnonymousFunctions Changes invalid input: '&' improvements: • Single linear pass (O(n) instead of repeated indexOf → much faster) • Cleaner structure with helper methods • Safer in-place replacement (no risky mutations during iteration) • Exact same replacement logic invalid input: '&' output as your original code • Better readability and maintainability • Preserves your original bracket/comma detection rules
      Parameters:
      scanner -
    • removeExcessBrackets

      public static void removeExcessBrackets(List<String> scanner)
      This technique will rid tokens of offending brackets up to the last bracket. It assumes that it knows the rules that allow one to remove all brackets from a token. One however needs check the ruls to be sure that there is no error. If this one messes up, please switch to the
      invalid reference
      MathScanner#$removeExcessBrackets(List)
      method.
      Parameters:
      scanner - The list of scanned tokens.
    • removeExcessBrackets1

      public static void removeExcessBrackets1(List<String> scanner)
      Deprecated.
      Parameters:
      scanner -
    • extractFunctionStringFromExpressionForMatrixMethods

      public static void extractFunctionStringFromExpressionForMatrixMethods(List<String> list)
      Analyzes the list and extracts the Function string from it.
      Parameters:
      list - The list to be analyzed. Direct examples would be: intg(@sin(x+1),4,7) intg(F,4,7) where F is a function that has been defined before in the workspace.. and so on. Simplifies the list to the form matrix_method(funcName,params)
    • extractFunctionStringFromExpressionForMatrixMethods1

      public static void extractFunctionStringFromExpressionForMatrixMethods1(List<String> list)
      Analyzes the list and extracts the Function string from it.
      Parameters:
      list - The list to be analyzed. Direct examples would be: intg(@sin(x+1),4,7) intg(F,4,7) where F is a function that has been defined before in the workspace.. and so on. Simplifies the list to the form matrix_method(funcName,params)
    • maskCommas

      public void maskCommas()
      Hides the commas in the scanner output as a double number which is not part of the output
    • unmaskCommas

      public void unmaskCommas()
      Brings back the original comma tokens from their masked state
    • main

      public static void main(String[] args)
      Parameters:
      args - Command line args (((2+3)^2))!-------((25))!-------