import java.io.*;
import java.util.*;
import javax.swing.*;

public class Calculator {

	private static Stack operators = new Stack();
	private static Stack operands = new Stack();
	
	
	public static void main(String args[]) throws IOException {
		
		JOptionPane.showMessageDialog(null,  "Hello user! I hope you have a great time using this.  = ]", "Welcome", JOptionPane.PLAIN_MESSAGE);
		
		JOptionPane.showMessageDialog(null,  "Note: Enter Infix expressions with spaces in between.", "Welcome", JOptionPane.PLAIN_MESSAGE);
		
		
		String infix = JOptionPane.showInputDialog("Enter Mathematical Expression Here: ");
		
		String output = "The expression in Postfix is: " +  convertToPostfix(infix);
		JOptionPane.showMessageDialog(null, output);
		
		String answer = "The answer to the equation:  " + evaluate(convertToPostfix(infix));
		JOptionPane.showMessageDialog(null, answer);
		
		String options[] = {"Yes","No",};
		
		int option = JOptionPane.showOptionDialog(null,"Do you want to evaluate another expression?", "Calculator", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,options,options[0]);
		
		JOptionPane.showMessageDialog(null,  "Thank you for using this calculator.\n Have a nice day!  = ]");
		
	}

	private static String convertToPostfix(String infix) {
			
		StringTokenizer s = new StringTokenizer(infix);
			
			String symbol;
			String postfix = "";
			
			while (s.hasMoreTokens()) {
				
				symbol = s.nextToken();
			
					if (Character.isDigit(symbol.charAt(0)))
				
						postfix = postfix + " " + (Integer.parseInt(symbol));
					
					else if (symbol.equals("(")){
						Character isOperator = new Character('(');
						operators.push(isOperator);
					}
					else if (symbol.equals(")")) {
						while (((Character)operators.peek()).charValue() != '(') {
							postfix = postfix + " " + operators.pop();
						}
						operators.pop();
					} else {
						while (!operators.empty() && !(operators.peek()).equals("(") && precedence(symbol.charAt(0)) <= precedence(((Character)operators.peek()).charValue()))
							postfix = postfix + " " + operators.pop();
							Character isOperator = new Character(symbol.charAt(0));
							operators.push(isOperator);
					}
			}
			while (!operators.empty())
				postfix = postfix + " " + operators.pop();
							return postfix;
		}

		private static int evaluate(String postfix) {
			
			StringTokenizer s = new StringTokenizer(postfix);
			int value;
			String symbol;
			
			while (s.hasMoreTokens()) {
				symbol = s.nextToken();
				if (Character.isDigit(symbol.charAt(0))) {
					Integer isOperand = new Integer(Integer.parseInt(symbol));
					operands.push(isOperand);
				}else{
					int op2 = ((Integer)operands.pop()).intValue();
					int op1 = ((Integer)operands.pop()).intValue();
					int result = 0;
					switch(symbol.charAt(0)){
					
						case '*': {result = op1 * op2; break;}
						case '+': {result = op1 + op2; break;}
						case '-': {result = op1 - op2; break;}
						case '/': {result = op1 / op2; break;}
						case '%': {result = op1 % op2; break;}
					}
					Integer isOperand = new Integer(result);
					operands.push(isOperand);
				}
			}
			value = ((Integer)operands.pop()).intValue();
			return value;
		}

		private static int precedence(char operator) {
			
			if (operator == '+' || operator == '-' )
				return 1;
			else if (operator == '*' || operator == '/' || operator == '%')
				return 2;
			return 0;
		}
	}

In my main method, the part were it ask the user if it wants to evaluate another expression, if YES it must go back to "enter mathematical expression" and evaluates again and if NO it should exit and show "thank you for using this calculator". My problem is that i don't know how to loop it.. any ideas anyone??

additional questions:

what i mean is that how can i loop my main method when the user wants to enter an input again..

The code posted at the top is a code that converts infix to postfix expressions. My problem is that in my main method, i can't get my "get input from user" to loop while the user still wants to input expressions. In line 28, if YES it should go back to lines 15-28, if NO then output line 30.

???????????????

The code posted at the top is a code that converts infix to postfix expressions. My problem is that in my main method, i can't get my "get input from user" to loop while the user still wants to input expressions. In line 28, if YES it should go back to lines 15-28, if NO then output line 30.

???????????????

Hello,

For now, i have 2 ways in my mind. (Recursive method & Do While loop).

1st One) Create a method and cut body of main method into it. Then on showOptionPane if user entered YES then call that same method from there (Recursion) else exit.

2nd One) Use Do While loop, So it will run minimum once and store input of showOptionPane in String variable ( EG: userContinue ) and While expression will be userContinue == "YES".

Regards, :)

public static void main(String args[]) throws IOException {
		
	JOptionPane.showMessageDialog(null,  "Hello user! I hope you have a wonderful time using the calculator.  = ]", "Welcome", JOptionPane.PLAIN_MESSAGE);	 
	  do{
	String options[] = {"Yes","No",};
	int option = JOptionPane.showOptionDialog(null,"Do you want evaluate an expression?", "Calculator", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,options,options[0]);  
	   
		    JOptionPane.showMessageDialog(null,  "Note: Enter Infix expressions with spaces in between.", "Welcome", JOptionPane.PLAIN_MESSAGE);
		    String infix = JOptionPane.showInputDialog("Enter Mathematical Expression Here: ");
		    String output = "The expression in Postfix is: " +  convertToPostfix(infix);
		    JOptionPane.showMessageDialog(null, output);
		    String answer = "The answer to the equation:  " + evaluate(convertToPostfix(infix));
		    JOptionPane.showMessageDialog(null, answer);
		}while (option == 0); 
		    JOptionPane.showMessageDialog(null,  "Thank you for using the calculator.\n Have a nice day!  = ]");
	}

i don't know much about recursive, so i choose do-while.. :)

i modified the the main method, error is that option cannot be resolved

I got my answer already.. thanks puneetkay..
however, in lines 51 and 62 has this comments : Type safety: The method push(Object) belongs to the raw type Stack. References to generic type
Stack<E> should be parameterized. As well as lines 80 and 94 has this comment: Type safety: The method push(Object) belongs to the raw type Stack. References to generic type Stack<E> should be parameterized. I understand that it should be parametized. But then if i do, the rest of my code makes a lot of errors.. what should i do?

GUYs, i figured about the raw type thing..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.