Java reverse a string using stack

Examples

package com.w3schools;
 
public class Test {
    private int stackSize;
    private char[] stackArray;
    private int top;
 
    /**
     * constructor to create stack with size
     * @param size
     */
    public Test(int size) {
        this.stackSize = size;
        this.stackArray = new char[stackSize];
        this.top = -1;
    }
 
    /**
     * Adds new entry to the top of the stack
     * @param entry
     * @throws Exception 
     */
    public void push(char entry) throws Exception {
    	this.stackArray[++top] = entry;
    }
 
    /**
     * Removes an entry from the top of the stack.
     * @return
     * @throws Exception 
     */
    public char pop() throws Exception {
        if(this.isStackEmpty()){
        	System.out.println("Stack underflow.");
        }
        return this.stackArray[top--];
    }
 
    /**
     * Returns top of the stack without removing it.
     * @return
     */
    public int peek() {
        return stackArray[top];
    }
 
    /**
     * Returns true if the stack is empty
     * @return
     */
    public boolean isStackEmpty() {
        return (top == -1);
    }
 
    /**
     * Returns true if the stack is full
     * @return
     */
    public boolean isStackFull() {
        return (top == stackSize - 1);
    }
 
    public String reverseWord(String word) throws Exception{        
        StringBuilder sb = new StringBuilder();
        int size = word.length();
        for(int i=0;i<size;i++){
            push(word.charAt(i));
        }
        while(!isStackEmpty()){
            sb.append(pop());
        }
        return sb.toString();
    }
 
	public static void main(String args[]){
		try {
			Test test = new Test(10);
			String word = "w3schools";
			System.out.println("Input word: " + word);
			System.out.println("Reversed word: "+test.reverseWord(word));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

Output

Input word: w3schools
Reversed word: tniops3w