can someone help me to fix my codes.I am receving some errors.This is my first time i am using stack in GUI.

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

import java.awt.event.*;

import javax.swing.*;

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


public class CalcGUIPanel extends JPanel
{




 //=============================================== instance variables

 //--\- Component referenced during execution

 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.setFont(BIGGER_FONT);

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);


 b.setFont(BIGGER_FONT);

 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);

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


 b.setFont(BIGGER_FONT);

 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;

 }

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

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

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

 }  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 7 Replies

what do you want to fix, I miss there question

what do you want to fix, I miss there question

these are the errors i am getting. I am not sure whats the reason.

\Users\Hamza\Pictures\CalcGUIPanel.java:252: 'catch' without 'try'
 } catch (NumberFormatException ex) {
   ^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:252: ')' expected
 } catch (NumberFormatException ex) {
                               ^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:252: not a statement
 } catch (NumberFormatException ex) {
         ^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:252: ';' expected
 } catch (NumberFormatException ex) {
                                  ^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:193: 'try' without 'catch' or 'finally'
try {
^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:310: reached end of file while parsing
}
 ^
6 errors

you have to check your curly brackets

you have to check your curly brackets

these are the errors I am getting.
C:\Users\Hamza\Pictures\CalcGUIPanel.java:43: cannot find symbol
symbol : variable BIGGER_FONT
location: class CalcGUIPanel
clearButton.setFont(BIGGER_FONT);
^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:63: array required, but java.lang.String found
cStack.push(buttonOrder);
^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:80: cannot find symbol
symbol : variable BIGGER_FONT
location: class CalcGUIPanel
b.setFont(BIGGER_FONT);
^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:113: cannot find symbol
symbol : variable c
location: class CalcGUIPanel
c.push("1");
^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:114: cannot find symbol
symbol : variable c
location: class CalcGUIPanel
c.push("2");
^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:115: cannot find symbol
symbol : variable c
location: class CalcGUIPanel
c.push("3");
^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:116: cannot find symbol
symbol : variable c
location: class CalcGUIPanel
c.push("4");
^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:117: cannot find symbol
symbol : variable c
location: class CalcGUIPanel
c.push("5");
^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:118: cannot find symbol
symbol : variable c
location: class CalcGUIPanel
c.push("6");
^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:119: cannot find symbol
symbol : variable c
location: class CalcGUIPanel
c.push("7");
^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:120: cannot find symbol
symbol : variable c
location: class CalcGUIPanel
c.push("8");
^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:121: cannot find symbol
symbol : variable c
location: class CalcGUIPanel
c.push("9");
^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:122: cannot find symbol
symbol : variable c
location: class CalcGUIPanel
c.push("0");
^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:123: cannot find symbol
symbol : variable c
location: class CalcGUIPanel
c.push("+");
^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:124: cannot find symbol
symbol : variable c
location: class CalcGUIPanel
c.push("-");
^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:125: cannot find symbol
symbol : variable c
location: class CalcGUIPanel
c.push("*");
^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:126: cannot find symbol
symbol : variable c
location: class CalcGUIPanel
c.push("/");
^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:127: cannot find symbol
symbol : variable c
location: class CalcGUIPanel
c.push("enter");
^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:130: cannot find symbol
symbol : variable BIGGER_FONT
location: class CalcGUIPanel
b.setFont(BIGGER_FONT);
^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:212: cannot find symbol
symbol : class BigInteger
location: class CalcGUIPanel.OpListener
BigInteger resultValue = new BigInteger(cStack.pop());
^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:212: cannot find symbol
symbol : class BigInteger
location: class CalcGUIPanel.OpListener
BigInteger resultValue = new BigInteger(cStack.pop());
^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:213: cannot find symbol
symbol : class BigInteger
location: class CalcGUIPanel.OpListener
BigInteger currentValue = new Biginteger(cStack.pop());
^
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:213: cannot find symbol
symbol : class Biginteger
location: class CalcGUIPanel.OpListener
BigInteger currentValue = new Biginteger(cStack.pop());
^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:222: cannot find symbol
symbol : class BigInteger
location: class CalcGUIPanel.OpListener
BigInteger resultValue = new BigInteger(cStack.pop());
^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:222: cannot find symbol
symbol : class BigInteger
location: class CalcGUIPanel.OpListener
BigInteger resultValue = new BigInteger(cStack.pop());
^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:223: cannot find symbol
symbol : class BigInteger
location: class CalcGUIPanel.OpListener
BigInteger currentValue = new BigInteger(cStack.pop());
^
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:223: cannot find symbol
symbol : class BigInteger
location: class CalcGUIPanel.OpListener
BigInteger currentValue = new BigInteger(cStack.pop());
^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:232: cannot find symbol
symbol : class BigInteger
location: class CalcGUIPanel.OpListener
BigInteger resultValue = new BigInteger(cStack.pop());
^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:232: cannot find symbol
symbol : class BigInteger
location: class CalcGUIPanel.OpListener
BigInteger resultValue = new BigInteger(cStack.pop());
^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:233: cannot find symbol
symbol : class BigInteger
location: class CalcGUIPanel.OpListener
BigInteger currentValue = new BigInteger(cStack.pop());
^
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:233: cannot find symbol
symbol : class BigInteger
location: class CalcGUIPanel.OpListener
BigInteger currentValue = new BigInteger(cStack.pop());
^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:243: cannot find symbol
symbol : class BigInteger
location: class CalcGUIPanel.OpListener
BigInteger resultValue = new BigInteger(cStack.pop());
^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:243: cannot find symbol
symbol : class BigInteger
location: class CalcGUIPanel.OpListener
BigInteger resultValue = new BigInteger(cStack.pop());
^
C:\Users\Hamza\Pictures\CalcGUIPanel.java:244: cannot find symbol
symbol : class BigInteger
location: class CalcGUIPanel.OpListener
BigInteger currentValue = new BigInteger(cStack.pop());
^
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:244: cannot find symbol
symbol : class BigInteger
location: class CalcGUIPanel.OpListener
BigInteger currentValue = new BigInteger(cStack.pop());
^
39 errors

Tool completed with exit code 1

cannot find symbol

The compiler can not find the definition for the variable shown in the error message.
You need to look at each error message, get the symbol that was not found, go into your code and find where it is defined. If it is not defined, you need to define it.
If it is defined make sure its definition is in scope with the place that you are trying to use it.

For classes that are not found, like BigInteger, you need to add an import statement for the package that the class is in to tell the compiler where to find the definition for the class. Go to the API doc, find the page for the class and look at the top of the page to see what package the class is in.

You should learn to compile the program more often to find each error as you make it instead of waiting to do your compile and then having too many errors all at one time.

The compiler can not find the definition for the variable shown in the error message.
You need to look at each error message, get the symbol that was not found, go into your code and find where it is defined. If it is not defined, you need to define it.
If it is defined make sure its definition is in scope with the place that you are trying to use it.

For classes that are not found, like BigInteger, you need to add an import statement for the package that the class is in to tell the compiler where to find the definition for the class. Go to the API doc, find the page for the class and look at the top of the page to see what package the class is in.

You should learn to compile the program more often to find each error as you make it instead of waiting to do your compile and then having too many errors all at one time.

I just update my codes.

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
{




 //=============================================== instance variables

 //--\- Component referenced during execution

 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();
}
}
}

These r errors i am getting.Plz also look at my stack.

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:213: cannot find symbol
symbol : class Biginteger
location: class CalcGUIPanel.OpListener
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;
^
10 errors

Tool completed with exit code 1

Most of the text of the error messages explain what the problem is.
You need to read the text of the error message and look at the statement with the error and see what error the compiler has found.

For example: operator / cannot be applied to
The compiler does not like your using the / operator. You need to use methods with objects. The / operator is for primitives like int and double.

array required, but java.lang.String found

You are using [] with a variable that is a String, NOT an array.

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.