Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //for loop to loop through char of string
- for(int i=0; i<expressionString.length(); i++) {
- //cast char into ascii int
- int ascii = (int) charAt(i);
- //appending to token if one of singly operator symbols: *,/,(,),[,]
- if(ascii == 40 || ascii == 41 || ascii == 42 || ascii == 47 || ascii == 91 || ascii == 93){
- token.append((char) ascii);
- tokenList.add(token.toString());
- } //append if +, -
- else if(ascii == 43 || ascii == 45) {
- token.append((char) ascii);
- //check next char if + or /, if so append to token again
- int nextChar = (char) charAt(i+1);
- if(nextChar == 43 || nextChar == 45) {
- token.append((char) nextChar);
- }
- tokenList.add(token.toString());
- } //appending to token if it's a num
- else if ( ascii >= 48 || ascii <=57) {
- token.append((char) ascii);
- //check if next char is a num
- while ((int) charAt(i+1) >= 48 || (int) charAt(i+1) <= 57) {
- //increment i in for loop to check
- i++;
- token.append((int) charAt(i));
- }
- tokenList.add(token.toString());
- }
- //
- }
Add Comment
Please, Sign In to add comment