src/evaluatePostfix.java
// USING NON-VOID METHOD WITH RETURN DOUBLE DATATYPE
USING NON-VOID METHOD WITH RETURN DOUBLE DATATYPE
USING NON - VOID METHOD WITH RETURN DOUBLE DATATYPE
public static Double evalPostfix(String postfix) {
        Stack<Double> myStack = new Stack<>();
        int charCount = postfix.length();
        try {
            for (int i = 0; i < charCount; i++) {
                char currentChar = postfix.charAt(i);
                char nextChar = ' ';
                if ((i + 1) < charCount) {
                    nextChar = postfix.charAt(i + 1);
                } 
                if (Character.isWhitespace(currentChar)) {
                    /*
                     * IF THE SCANNED CHARACTER IS A BLANK CHAR
                     * THEN DO NOTHING
                     */
                } else if (Character.isDigit(currentChar)) {
                    /*
                     * IF THE SCANNED CHARACTER HAS DIGIT
                     * VALUE THEN PUSH THE VALUE TO THE STACK
                     */
                    Double num = 0.0;
                    String appendNum = "";
                    /*
                     * USING WHILE LOOP TO DETERMINE IF THE INPUT
                     * HAS MORE THAN 1 DIGIT
                     */
                    while (Character.isDigit(currentChar)) {
                        appendNum += Character.getNumericValue(currentChar);
                        num = Double.parseDouble(appendNum);
                        i++;
                        currentChar = postfix.charAt(i);
                    } 
                    i--;
                    myStack.push(num);
                } else {
                    /* IF THE CURRENT CHAR IS AN OPERATOR */
                    /*
                     * USING IF STATEMENT TO CHECK IF THE CURRENT CHAR
                     * IS AN OPERATOR OR IT HAS DIGIT VALUE AFTER THAT
                     * IF HAS VALUE(S) AFTER THE CHAR THEN PROBABLY
                     * IT IS A NEGATIVE NUMBER. PUSH THE NUMBER INTO
                     * THE STACK
                     */
                    if (Character.isDigit(nextChar)) {
                        Double num = 0.0;
                        String appendNum = Character.toString(currentChar);
                        currentChar = postfix.charAt(i + 1);
                        while (Character.isDigit(currentChar)) {
                            appendNum += currentChar;
                            num = Double.parseDouble(appendNum);
                            i = i + 2;
                            currentChar = postfix.charAt(i);
                        } 
                        i--;
                        myStack.push(num);
                    } else {
                        /*
                         * ELSE IF IT IS JUST AN OPERATOR,
                         * POP TWO ELEMENTS FROM THE TOP OF THE STACK
                         * AND STORE IT IN THE VARIABLES
                         */
                        Double operand1 = myStack.pop();
                        Double operand2 = myStack.pop();
                        Double result = 0.0;
                        /*
                         * DO A MATHEMATICAL OPERATION BASED
                         * ON THE CURRENT OPERATOR CHARACTER
                         */
                        switch (currentChar) {
                            case '+':
                                result = operand2 + operand1;
                                break;
                            case '-':
                                result = operand2 - operand1;
                                break;
                            case '/':
                                result = operand2 / operand1;
                                break;
                            case '*':
                                result = operand2 * operand1;
                                break;
                        }
                        /*
                         * FINALLY PUSH THE FINAL CALCULATION
                         * RESULT INTO THE STACK
                         */
                        myStack.push(result);
                    } 
                } 
            } 
        } catch (StringIndexOutOfBoundsException e) {
        } catch (EmptyStackException e) {
            System.out.println("Error : Invalid Postfix Expression !");
        } catch (NumberFormatException e) {
            System.out.println("Error : Invalid Postfix Expression !");
        } 
        return myStack.pop();
    }
public static Double evalPostfix ( String postfix ) { Stack < Double > myStack = new Stack < > ( ) ; int charCount = postfix . length ( ) ; try { for ( int i = 0 ; i < charCount ; i ++ ) { char currentChar = postfix . charAt ( i ) ; char nextChar = ' ' ; if ( ( i + 1 ) < charCount ) { nextChar = postfix . charAt ( i + 1 ) ; } if ( Character . isWhitespace ( currentChar ) ) { /*
                     * IF THE SCANNED CHARACTER IS A BLANK CHAR
                     * THEN DO NOTHING
                     */ } else if ( Character . isDigit ( currentChar ) ) { /*
                     * IF THE SCANNED CHARACTER HAS DIGIT
                     * VALUE THEN PUSH THE VALUE TO THE STACK
                     */ Double num = 0.0 ; String appendNum = "" ; /*
                     * USING WHILE LOOP TO DETERMINE IF THE INPUT
                     * HAS MORE THAN 1 DIGIT
                     */ while ( Character . isDigit ( currentChar ) ) { appendNum += Character . getNumericValue ( currentChar ) ; num = Double . parseDouble ( appendNum ) ; i ++ ; currentChar = postfix . charAt ( i ) ; } i -- ; myStack . push ( num ) ; } else { /* IF THE CURRENT CHAR IS AN OPERATOR */ /*
                     * USING IF STATEMENT TO CHECK IF THE CURRENT CHAR
                     * IS AN OPERATOR OR IT HAS DIGIT VALUE AFTER THAT
                     * IF HAS VALUE(S) AFTER THE CHAR THEN PROBABLY
                     * IT IS A NEGATIVE NUMBER. PUSH THE NUMBER INTO
                     * THE STACK
                     */ if ( Character . isDigit ( nextChar ) ) { Double num = 0.0 ; String appendNum = Character . toString ( currentChar ) ; currentChar = postfix . charAt ( i + 1 ) ; while ( Character . isDigit ( currentChar ) ) { appendNum += currentChar ; num = Double . parseDouble ( appendNum ) ; i = i + 2 ; currentChar = postfix . charAt ( i ) ; } i -- ; myStack . push ( num ) ; } else { /*
                         * ELSE IF IT IS JUST AN OPERATOR,
                         * POP TWO ELEMENTS FROM THE TOP OF THE STACK
                         * AND STORE IT IN THE VARIABLES
                         */ Double operand1 = myStack . pop ( ) ; Double operand2 = myStack . pop ( ) ; Double result = 0.0 ; /*
                         * DO A MATHEMATICAL OPERATION BASED
                         * ON THE CURRENT OPERATOR CHARACTER
                         */ switch ( currentChar ) { case '+' : result = operand2 + operand1 ; break ; case '-' : result = operand2 - operand1 ; break ; case '/' : result = operand2 / operand1 ; break ; case '*' : result = operand2 * operand1 ; break ; } /*
                         * FINALLY PUSH THE FINAL CALCULATION
                         * RESULT INTO THE STACK
                         */ myStack . push ( result ) ; } } } } catch ( StringIndexOutOfBoundsException e ) { } catch ( EmptyStackException e ) { System . out . println ( "Error : Invalid Postfix Expression !" ) ; } catch ( NumberFormatException e ) { System . out . println ( "Error : Invalid Postfix Expression !" ) ; } return myStack . pop ( ) ; }
USING NON-VOID METHOD WITH RETURN DOUBLE DATATYPE
USING NON - VOID METHOD WITH RETURN DOUBLE DATATYPE
// CREATE STACK OBJECT // STORE CHARACTER COUNT // USING FOR LOOP TO SCAN EACH CHARACTER IN A STRING // TO PREVENT INDEXOUT EXCEPTION // END IF // END DO // PUSH THE DIGIT VALUE INTO THE STACK // STORE OPERAND INTO VARIABLE // END DO // PUSH THE NEGATIVE VALUE INTO THE STACK // END SWITCH // END IF // END IF // END FOR LOOP // USING TRY CATCH BLOCK TO HANDLE ERROR/EXCEPTION // DO NOTHING // TO AVOID EXCEPTION OF UNUSED VALUE // END TRY CATCH // GET RETURN VALUE OF TOP OF THE STACK (THE RESULT)