private Stack<CalculationToken> tokenize(String expression) throws ParsingException {
  if (expression == null) {
    throw new ParsingException("Expression is Null");
  }
  if (expression.isEmpty()) {
    throw new ParsingException("Expression is Empty");
  }
  Stack<CalculationToken> calculationTokenStack=new Stack<>();
  Character prevToken='\0';
  int bracketSum=0;
  for (expressionIterator=0; expressionIterator < expression.length(); ++expressionIterator) {
    Character c=expression.charAt(expressionIterator);
switch (c) {
case ' ':
case '\n':
case '\t':
      continue;
case '.':
    throw new ParsingException(ILLEGAL_POSITION_IN_EXPRESSION + " . ");
default :
  break;
}
if (!cIsArithmetic(c) && c != '(' && c != ')' && !Character.isDigit(c)) {
throw new ParsingException("Unexpected symbol");
}
if (Character.isDigit(c) && (prevToken == 'n' || prevToken == ')')) {
throw new ParsingException(ILLEGAL_POSITION_IN_EXPRESSION + " number in unexpected place ");
}
if (Character.isDigit(c)) {
Double number=readDoubleNumberFromExpression(expression);
calculationTokenStack.push(new DoubleToken(number));
prevToken='n';
continue;
}
if (prevToken == 'n' && c == '(') {
throw new ParsingException(ILLEGAL_POSITION_IN_EXPRESSION + " number in unexpected place ");
}
if ((prevToken == '\0' || prevToken == '(') && (c != '-' && c != '(')) {
throw new ParsingException(ILLEGAL_POSITION_IN_EXPRESSION + c);
}
if (cIsArithmetic(prevToken) && cIsArithmetic(c) && c != '-') {
throw new ParsingException(ILLEGAL_POSITION_IN_EXPRESSION + prevToken + c);
}
calculationTokenStack.push(new OperationToken(c));
if (c == '(') {
++bracketSum;
}
 else if (c == ')') {
--bracketSum;
}
prevToken=c;
}
if (cIsArithmetic(prevToken)) {
throw new ParsingException("Expression ends with an unfinished arithmetic expression");
}
if (bracketSum != 0) {
throw new ParsingException("Expression contains a Faulty Bracket Sequence");
}
return calculationTokenStack;
}

private Stack < CalculationToken > tokenize ( String expression ) throws ParsingException { if ( expression == null ) { throw new ParsingException ( "Expression is Null" ) ; } if ( expression . isEmpty ( ) ) { throw new ParsingException ( "Expression is Empty" ) ; } Stack < CalculationToken > calculationTokenStack = new Stack < > ( ) ; Character prevToken = '\0' ; int bracketSum = 0 ; for ( expressionIterator = 0 ; expressionIterator < expression . length ( ) ; ++ expressionIterator ) { Character c = expression . charAt ( expressionIterator ) ; switch ( c ) { case ' ' : case '\n' : case '\t' : continue ; case '.' : throw new ParsingException ( ILLEGAL_POSITION_IN_EXPRESSION + " . " ) ; default : break ; } if ( ! cIsArithmetic ( c ) && c != '(' && c != ')' && ! Character . isDigit ( c ) ) { throw new ParsingException ( "Unexpected symbol" ) ; } if ( Character . isDigit ( c ) && ( prevToken == 'n' || prevToken == ')' ) ) { throw new ParsingException ( ILLEGAL_POSITION_IN_EXPRESSION + " number in unexpected place " ) ; } if ( Character . isDigit ( c ) ) { Double number = readDoubleNumberFromExpression ( expression ) ; calculationTokenStack . push ( new DoubleToken ( number ) ) ; prevToken = 'n' ; continue ; } if ( prevToken == 'n' && c == '(' ) { throw new ParsingException ( ILLEGAL_POSITION_IN_EXPRESSION + " number in unexpected place " ) ; } if ( ( prevToken == '\0' || prevToken == '(' ) && ( c != '-' && c != '(' ) ) { throw new ParsingException ( ILLEGAL_POSITION_IN_EXPRESSION + c ) ; } if ( cIsArithmetic ( prevToken ) && cIsArithmetic ( c ) && c != '-' ) { throw new ParsingException ( ILLEGAL_POSITION_IN_EXPRESSION + prevToken + c ) ; } calculationTokenStack . push ( new OperationToken ( c ) ) ; if ( c == '(' ) { ++ bracketSum ; } else if ( c == ')' ) { -- bracketSum ; } prevToken = c ; } if ( cIsArithmetic ( prevToken ) ) { throw new ParsingException ( "Expression ends with an unfinished arithmetic expression" ) ; } if ( bracketSum != 0 ) { throw new ParsingException ( "Expression contains a Faulty Bracket Sequence" ) ; } return calculationTokenStack ; }
tokenize expression into a stack of numbers and operations . check that expression contains only recognised symbols in a readable sequence , and has a correct bracket sequence .
