Hello,

I'm creating a windows like calculator using Java, I seem to be having some problems..

It will let me calculate two numbers together e.g.

2 + 2 (It will display 4)

however, if I want to do multiple calculations... e.g.

2 + 2 + * 3

It will only still add the two numbers, which I have assigned to number1 and number2

Any suggestions?

would you kindly post the code so we can help, thanks

public class MycalDesign extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1388448113487969657L;
	private String numDisplayed;
	private String digit;
	private double number1;
	private double number2;
	private double result;
	private double finalResult;
	private double numCheck;
	private boolean nextNum = true;
	private double otherCalFunctions;

	// private CalOp calculation = new CalOp();
	private ArrayList<Double> op;
	// private String [] listData = historyComputations.toArray(new
	// String[historyComputations.size()]);

	private JTextArea calDisplay;
	private JTextArea formulaDisplay;
	private JList historyList;
	private JPanel insideCalPanel;

	public static void main(String[] args) {
		MycalDesign frame_1 = new MycalDesign();
		frame_1.setVisible(true);
		frame_1.setSize(300, 300);
		frame_1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame_1.setResizable(false);
		frame_1.pack();

	}

	public MycalDesign() {
		super("Calculator");
		getContentPane().setLayout(new BorderLayout());
		insideCalPanel = new JPanel(new BorderLayout());
		numPadGUI();
		funcPadGUI();
		memPadGUI();
		historyMemoryList();
		op = new ArrayList<Double>();
		calDisplay = new JTextArea();
		calDisplay.setEditable(false);
		formulaDisplay = new JTextArea();
		formulaDisplay.setEditable(false);
		formulaDisplay.setBackground(new Color(195, 188, 188));
		//formulaDisplay.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
		calDisplay.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
		calDisplay.setBackground(new Color(195, 188, 188));
		// formulaDisplay.setForeground(Color.WHITE);
		insideCalPanel.add(calDisplay, BorderLayout.NORTH);
		getContentPane().add(insideCalPanel, BorderLayout.CENTER);
		getContentPane().add(formulaDisplay, BorderLayout.NORTH);

	}

	public void numPadGUI() {
		final ActionListener numPadListener = new ActionListener() {

		
	@Override
			public void actionPerformed(ActionEvent e) {
				digit = e.getActionCommand();
				nextNum = false;
				if (nextNum) {
				
	calDisplay.setText(digit);
					nextNum = true;
				} else {
					calDisplay.setText(calDisplay.getText() + digit);
					numDisplayed = calDisplay.getText();
				
	number1 = readDisplayNumber();
				}
				formulaDisplay.append(digit);
				
			}
		};
		JPanel numPadPanel = new JPanel(new GridLayout(4, 3, 1, 1));
		String numPad = "123456789.0C";
		for (int i = 0; i < numPad.length(); i++) {
			JButton numButton = new JButton(numPad.substring(i, i + 1));
		
	numButton.addActionListener(numPadListener);
			numPadPanel.add(numButton);
		}
		insideCalPanel.add(numPadPanel, BorderLayout.CENTER);

	}

	public void funcPadGUI() {
		ActionListener funcPadListener = new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				
				String operator = e.getActionCommand();
				actionClear();
				
				if (operator.equals("+")) {


				} else if (operator.equals("/")) {

					formulaDisplay.append(" / ");

				} else if (operator.equals("-")) {

					formulaDisplay.append(" - ");

				} else if (operator.equals("x")) {

					formulaDisplay.append(" x ");

				} else if (operator.equals("%")) {

				} else if (operator.equals("sqrt")) {

				} else if (operator.equals("x^2")) {

				} else if (operator.equals("bk")) {

				} else if (operator.equals("=")) {

					

					if (numCheck == 1) {
						calDisplay.setText(Double.toString(result));
					} else if (numCheck == 2) {

					} else if (numCheck == 3) {

					} else if (numCheck == 4) {

				
	}
				}
			}

		};
		JPanel funcPadPanel = new JPanel(new GridLayout(4, 1, 1, 1));
		String[] funcPad = { "+", "sqrt", "x", "x^2", "-", "%", "/", "=" };
		for (int i = 0; i < funcPad.length; i++) {
			JButton funcButton = new JButton(funcPad[i]);
			funcButton.addActionListener(funcPadListener);
			funcPadPanel.add(funcButton);
		}
		insideCalPanel.add(funcPadPanel, BorderLayout.EAST);

	}

	public void memPadGUI() {

		JPanel memPadPanel = new JPanel(new GridLayout(4, 1, 1, 1));
		String[] memPad = { "MR", "MC", "M+", "M-" };
		for (int i = 0; i < memPad.length; i++) {
			JButton memButton = new JButton(memPad[i]);
			memButton.addActionListener(memPadListener);
			memPadPanel.add(memButton);
		}
		insideCalPanel.add(memPadPanel, BorderLayout.WEST);
	}


	public void actionClear() {
		calDisplay.setText("");

	}

	public double readDisplayNumber() {
		double num1;
		num1 = Double.valueOf(numDisplayed);
		return num1;
	}


	}

}

Any help would be much appreciated, the empty if, else statements i'm guessing is where I need to do everything.

I've tried, and got it to work using two numbers but I have deleted that now..

Member Avatar for ztini

Think of a calculator as having 3 parts:

left operand + operator + right operand.

5 + 3 * 2

5 is left, + is operator, 3 is right
perform calculation, send to output

Output becomes left operand.
so then you have 8 left, * operator, 2 right
perform calculation, send to output

if = is entered, it just repeats whatever is stored in left operand with operator and right operand.

One suggestion. Abstract out an "Operator" class that contains an abstract method execute(). Then you can create a method to create a JButton with an actionListener that invokes that abstract execute method.

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.