Hi i m creating simple rpn calculator.I m getting following errors.
C:\Users\Hamza\Pictures\CalcGUIPanel.java:63: array required, but java.lang.String found
cStack.push(buttonOrder);
^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:213: currentValue is already defined in actionPerformed(java.awt.event.ActionEvent)
BigInteger currentValue = new BigInteger(cStack.pop());
^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:215: operator + cannot be applied to java.math.BigInteger,java.math.BigInteger
resultValue += currentValue;
^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:223: currentValue is already defined in actionPerformed(java.awt.event.ActionEvent)
BigInteger currentValue = new BigInteger(cStack.pop());
^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:225: operator - cannot be applied to java.math.BigInteger,java.math.BigInteger
resultValue -= currentValue;
^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:233: currentValue is already defined in actionPerformed(java.awt.event.ActionEvent)
BigInteger currentValue = new BigInteger(cStack.pop());
^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:236: operator * cannot be applied to java.math.BigInteger,java.math.BigInteger
resultValue *= currentValue;
^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:244: currentValue is already defined in actionPerformed(java.awt.event.ActionEvent)
BigInteger currentValue = new BigInteger(cStack.pop());
^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:245: operator / cannot be applied to java.math.BigInteger,java.math.BigInteger
resultValue /= currentValue;
^
9 errors

Tool completed with exit code 1

import java.util.Stack;
import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;
import java.util.Scanner;
import java.math.BigInteger;

public class CalcGUIPanel extends JPanel
{

 private JTextField displayField; // display result / input.

 //--\- Variables representing state of the calculator

 private boolean startNumber = true; // true: num key next

 private int resultValue = 0; // result so far

 private String previousOp = "="; // previous operation

 Stack<String> cStack=new Stack<String>();

 public CalcGUIPanel()
 {

 //--\- Display field

 displayField = new JTextField();

JButton clearButton = new JButton("CLEAR");



clearButton.addActionListener(new ClearListener());



 //--\- One listener for all numeric keys.

 ActionListener numListener = new NumListener();

 //--\- Layout numeric keys in a grid. Generate the buttons

 // in a loop from the chars in a string.

 String buttonOrder = "789456123 0 ";

 JPanel buttonPanel = new JPanel(new GridLayout(5, 3));

 for (int i = 0; i < buttonOrder.length(); i++) {
 {
cStack.push(buttonOrder[i]);
}


 String keyTop = buttonOrder.substring(i, i+1);

 if (keyTop.equals(" ")) {

 buttonPanel.add(new JLabel(""));

 } else {

 JButton b = new JButton(keyTop);

 b.addActionListener(numListener);




 buttonPanel.add(b);

 }

 }


 //--\- One ActionListener to use for all operator buttons.

 ActionListener opListener = new OpListener();

 //--\- Create panel with gridlayout to hold operator buttons.

 // Use array of button names to create buttons in a loop.

 JPanel opPanel = new JPanel(new GridLayout(5, 1));

 String[] opOrder = {"+", "-", "*", "/", "enter"};

 for (int i = 0; i < opOrder.length; i++) {

 JButton b = new JButton(opOrder[i]);
  {

 cStack.push(opOrder[i]);
 		 }



 b.addActionListener(opListener);

cStack.push("1");
cStack.push("2");
cStack.push("3");
cStack.push("4");
cStack.push("5");
cStack.push("6");
cStack.push("7");
cStack.push("8");
cStack.push("9");
cStack.push("0");
 cStack.push("+");
  cStack.push("-");
  cStack.push("*");
  cStack.push("/");
  cStack.push("enter");




 opPanel.add(b);

 }

 //--\- Layout the top-level panel.

 this.setLayout(new BorderLayout());

 this.add(displayField, BorderLayout.NORTH );

 this.add(buttonPanel , BorderLayout.CENTER);

 this.add(opPanel , BorderLayout.EAST );

this.add(clearButton , BorderLayout.SOUTH );

 }//end constructor

 //====================================================== action_clear

 /*\* Called by Clear btn action listener and elsewhere.*/

 private void action_clear() {

 startNumber = true;

 displayField.setText("0");

 resultValue = 0;

 previousOp = "=";

 }

 // inner listener class OpListener

 /*\* Listener for all op buttons. \*/

 class OpListener implements ActionListener {

 public void actionPerformed(ActionEvent e) {

 // The calculator is always in one of two states.

 // 1. A number must be entered \-\- this operator is wrong.

 // 2. An operator must be entered \-\- we're ok.
 if (startNumber) { // Error: needed number, not operator

 action_clear();

 displayField.setText("ERROR");

 } else {



 startNumber = true; // Next thing must be a number


try {

 String displayText = displayField.getText();

 int currentValue = Integer.parseInt(displayText);


 if (previousOp.equals("=")) {
 cStack.push(resultValue + "");



 resultValue = currentValue;

 }

  else if (previousOp.equals("+")) {
	  cStack.pop().equals("+");


 BigInteger resultValue = new BigInteger(cStack.pop());
 BigInteger currentValue = new BigInteger(cStack.pop());

 resultValue += currentValue;
 cStack.push(resultValue + "");


 }  else if (previousOp.equals("-")) {
	  cStack.pop().equals("-");

		 BigInteger resultValue = new BigInteger(cStack.pop());
		 BigInteger currentValue = new BigInteger(cStack.pop());

 resultValue -= currentValue;
 cStack.push(resultValue + "");


 } else if (previousOp.equals("*")) {
	cStack.pop().equals("*");

			 BigInteger resultValue = new BigInteger(cStack.pop());
			 BigInteger currentValue = new BigInteger(cStack.pop());


 resultValue *= currentValue;
 cStack.push(resultValue + "");


 } else if (previousOp.equals("/")) {
	cStack.pop().equals("/");

		 BigInteger resultValue = new BigInteger(cStack.pop());
			 BigInteger currentValue = new BigInteger(cStack.pop());
 resultValue /= currentValue;
 cStack.push(resultValue + "");

 }

 displayField.setText("" + resultValue);
 } catch (NumberFormatException ex) {

 action_clear();

 displayField.setText("Error");

}


 //--\- set \_previousOp for the next operator.

 previousOp = e.getActionCommand();

 }//endif \_startNumber

 }//endmethod

 }//end class

 //////////////////////////////////// inner listener class ClearListener

 // Action listener for numeric keys

 class NumListener implements ActionListener {

 public void actionPerformed(ActionEvent e) {

 String digit = e.getActionCommand(); // Get text from button

 if (startNumber) {

 // This is the first digit, clear field and set

 displayField.setText(digit);

 startNumber = false;

 } else {

 // Add this digit to the end of the display field

 displayField.setText(displayField.getText() + digit);

 }

 }

 }//end class

 //inner listener class ClearListener

class ClearListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

action_clear();
}
}
}

Recommended Answers

All 2 Replies

Many of the problems seem to be explained by the error message.
Which of the errors don't you understand?

C:\Users\Hamza\Pictures\CalcGUIPanel.java:63: array required, but java.lang.String found
cStack.push(buttonOrder);
^

What is buttonOrder? An array or ???
C:\Users\Hamza\Pictures\CalcGUIPanel.java:213: currentValue is already defined in actionPerformed(java.awt.event.ActionEvent)
BigInteger currentValue = new BigInteger(cStack.pop());
^

Remove one of the definitions of currentValue
C:\Users\Hamza\Pictures\CalcGUIPanel.java:215: operator + cannot be applied to java.math.BigInteger,java.math.BigInteger
resultValue += currentValue;
^

You need to use a method with objects. The + operator is for primitives.
C:\Users\Hamza\Pictures\CalcGUIPanel.java:223: currentValue is already defined in actionPerformed(java.awt.event.ActionEvent)
BigInteger currentValue = new BigInteger(cStack.pop());
^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:225: operator - cannot be applied to java.math.BigInteger,java.math.BigInteger
resultValue -= currentValue;
^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:233: currentValue is already defined in actionPerformed(java.awt.event.ActionEvent)
BigInteger currentValue = new BigInteger(cStack.pop());
^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:236: operator * cannot be applied to java.math.BigInteger,java.math.BigInteger
resultValue *= currentValue;
^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:244: currentValue is already defined in actionPerformed(java.awt.event.ActionEvent)
BigInteger currentValue = new BigInteger(cStack.pop());
^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:245: operator / cannot be applied to java.math.BigInteger,java.math.BigInteger
resultValue /= currentValue;
^
9 errors

The rest of the errors are repeats of the earlier ones.

A suggestion: Compile your program more often as you type it in. You have over 300 lines of code here. You should have started compiling when you had 50 lines and fixed the errors as you went along. Don't wait.

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.