Does anyone know how you could process an equation, or even get an equation with more than two operands? Right now, in my calculator, I can only have two operands, and I want to have more.

Right here is what I have:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Calculator extends JFrame implements ActionListener
{
	JButton[] btnNums;
	JButton btnBack;
	JButton btnClear;
	JButton btnCalculate;
	String[] strNames;
	String strOperand1;
	String strOperand2;
	Boolean wasClicked;
	String answer;
	double dd;

	final String STR_MULTIPLY = "MULTIPLY";
	final String STR_DIVIDE = "DIVIDE";
	final String STR_ADD = "ADD";
	final String STR_SUBTRACT = "SUBTRACT";

	String whichOperator;
	
	
	JTextField txtDisplay;
	
	public Calculator()
	{
		Container content = getContentPane();
		setSize(210,250);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		
		/*  construct a JPanel and the textfield that 
		 *  will show the inputed values and outputed 
		 *  results
		 */
		JPanel jpDisplay = new JPanel();
		jpDisplay.setLayout(new BorderLayout());
		txtDisplay = new JTextField(15);
		txtDisplay.setHorizontalAlignment(JTextField.RIGHT);
		jpDisplay.add(txtDisplay);
		
		/*  contstruct a JPanel that will contain a back
		 *  button and a clear button.
		 */
		JPanel jpRow2 = new JPanel();
		btnBack = new JButton("Backspace");
		btnBack.addActionListener(this);
		btnClear = new JButton("Clear");
		btnClear.addActionListener(this);
		btnClear.addActionListener(this);
		jpRow2.add(btnBack);
		jpRow2.add(btnClear);
		
		/*  construct a string array with all the names of the
		 *  buttons, then in a for loop create the new buttons
		 *  and add their name and an actionListener to them.
		 */
		String[] strNames = {"7","8", "9","/", "4", "5", "6","*", "1",
					   "2", "3","+", "0", "+/-", ".", "-"};
		btnNums = new JButton[16];
		JPanel jpButtons = new JPanel();
		jpButtons.setLayout(new GridLayout(4,4));
		
		  for (int i = 0; i < 16; i++)
		  {
			  btnNums[i] = new JButton(strNames[i]);
			  btnNums[i].addActionListener(this);
			  jpButtons.add(btnNums[i]);
		  }
		  
		  /*  construct the final JPanel and add a 
		   *  calculate button to it.
		   */
		  JPanel jpLastRow = new JPanel();
		  btnCalculate = new JButton("Calculate");
		  btnCalculate.addActionListener(this);
		  jpLastRow.add(btnCalculate);
		  
		  
		  //make wasClicked have a default value of false
		  wasClicked = false;
		  
		  /*  set the contentPane and create the layout
		   *  add the panels to the container
		   */
		  setContentPane(content);
		  setLayout(new FlowLayout());
		  setResizable(false);
		  content.add(jpDisplay, BorderLayout.NORTH);
		  content.add(jpRow2);
		  content.add(jpButtons);
		  content.add(jpLastRow);
		  setTitle("Mini Calculator");
		  setVisible(true); 
		  
	}
	
 
	public void actionPerformed(ActionEvent ae)
	{
		// if any of the array buttons was clicked append the button's text
		for (int i =0; i < 16; i++)
		{
			if (ae.getSource() == btnNums[i])
			{
				txtDisplay.setText(txtDisplay.getText()
						+ btnNums[i].getText());
				
			}
		}
		
		
		/*  if the backspace button was clicked, then create substring
		 *  to to subtract the farthest character to the right
		 */
		if (ae.getSource() == btnBack)
		{
			String strAll = txtDisplay.getText();
			int length = strAll.length();
			String strShowThis = strAll.substring(0,length - 1);
			txtDisplay.setText(strShowThis);
		}
			else if (ae.getSource() == btnClear)
			{
				txtDisplay.setText(null);
			}	
			else if (ae.getSource() == btnCalculate)
			{
				String strText = txtDisplay.getText();
				try 
				{
					int equation = Integer.parseInt(strText);
					txtDisplay.setText("" + equation);
				}
				catch (ArithmeticException aee)
				{
					aee.printStackTrace();
		 	 }
			
			}
			
			/*  start the process of finding which operator was 
			 *  chosen, and the process the equation in a new 
			 *  method
			 */
			 
			 if (ae.getSource() == btnNums[3])
			 {
				 processDivide();
			 }
			 else if (ae.getSource() == btnNums[7])
			 {
				 processMultiply();
			 }
			 else if (ae.getSource() == btnNums[11])
			 {
				 processAdd();
			 }
			 else if (ae.getSource() == btnNums[13])
			 {
				processPosOrNeg();
			 }
			 else if (ae.getSource() == btnNums[15])
			 {
				 processSubtract();
			 }
			 else
			 {
			 }

		// and finally process the calculation
		if (ae.getSource() == btnCalculate)
		{
			processCalc();
		}
		else
		{
		}
	}
	
	public void processDivide()
	{
		whichOperator = STR_DIVIDE;
		strOperand1 = txtDisplay.getText();
		int x = strOperand1.length();
		String s = strOperand1.substring(0, x - 1);
		dd = Double.parseDouble(s);
		txtDisplay.setText(null);
	}
	public void processMultiply()
	{
		whichOperator = STR_MULTIPLY;
		strOperand1 = txtDisplay.getText();
		int x = strOperand1.length();
		String s = strOperand1.substring(0, x - 1);
		dd = Double.parseDouble(s);
		txtDisplay.setText(null);
	}
	public void processAdd()
	{
		whichOperator = STR_ADD;
		strOperand1 = txtDisplay.getText();
		int x = strOperand1.length();
		System.out.println("variable x = " + x);
		String s = strOperand1.substring(0, x - 1);
		System.out.println("variable s = " + s);
		dd = Double.parseDouble(s);
		System.out.println("variable d = " + dd);
		txtDisplay.setText(null);
	}
	public void processPosOrNeg()
	{
	}
	public void processSubtract()
	{
		whichOperator = STR_SUBTRACT;
		strOperand1 = txtDisplay.getText();
		int x = strOperand1.length();
		String s = strOperand1.substring(0, x - 1);
		dd = Double.parseDouble(s);
		txtDisplay.setText(null);
	}

	public void processCalc()
	{
		strOperand2 = txtDisplay.getText();
		int x = strOperand2.length();
		String s = strOperand2.substring(0, x);
		double d = Double.parseDouble(s);
		
		if (whichOperator == STR_SUBTRACT)
		{
			double a = dd - d;
			txtDisplay.setText(dd + "-" + d + " = " + a);
		}
		else if (whichOperator == STR_ADD)
		{
			double a = dd + d;
			txtDisplay.setText(dd + "+" + d + " = " + a);
		}
		else if (whichOperator == STR_MULTIPLY)
		{
				double a = dd * d;
			txtDisplay.setText(dd + "*" + d + " = " + a);
		}
		else if (whichOperator == STR_DIVIDE)
		{
				double a = dd / d;
			txtDisplay.setText(dd + "/" + d + " = " + a);
		}
		else 
		{
		}
	
	}
	
	public static void main(String[] args)
	{
		Calculator calc = new Calculator();
	}
}

I'm welcome to anyother suggestions, and would appreciate them.

That's the problem with your purely linear way of doing things.
Create a parsetree and recursively process it from the inside out.

That's the problem with your purely linear way of doing things.
Create a parsetree and recursively process it from the inside out.

Yep, I knew the way I was doing would only allow two operands, but that's really all I could think of. I'll try the parsetree as you suggested..Sounds like something trivial, so I might have to come back for help!

This article has been dead for over six months. Start a new discussion instead.