# help w/ calculator gui



## tovachanah (Feb 15, 2006)

the problem is in the action handler
i am receiving the following errors
oh and by the way any tips will be aprreciated

----jGRASP exec: javac -g E:\homework for java class\Programs\Program3\Calculator.java

Calculator.java:94: missing method body, or declare abstract
public double ParseAndPerformAction (String cString, char operator); 
^
Calculator.java:101: cannot find symbol
symbol : variable cString
location: class Calculator.AddHandler
operand1 = createOperand (cString, operator,-1); 
^
Calculator.java:101: cannot find symbol
symbol : variable operator
location: class Calculator.AddHandler
operand1 = createOperand (cString, operator,-1); 
^
Calculator.java:102: cannot find symbol
symbol : variable cString
location: class Calculator.AddHandler
operand2 = createOperand (cString, ichar,cString.indexOf(operator));
^
Calculator.java:102: cannot find symbol
symbol : variable ichar
location: class Calculator.AddHandler
operand2 = createOperand (cString, ichar,cString.indexOf(operator));
^
Calculator.java:102: cannot find symbol
symbol : variable operator
location: class Calculator.AddHandler
operand2 = createOperand (cString, ichar,cString.indexOf(operator));
^
Calculator.java:102: cannot find symbol
symbol : variable cString
location: class Calculator.AddHandler
operand2 = createOperand (cString, ichar,cString.indexOf(operator));
^
Calculator.java:104: cannot find symbol
symbol : variable operator
location: class Calculator.AddHandler
switch (operator)
^
Calculator.java:123: return outside method
return answer;
^
Calculator.java:127: missing method body, or declare abstract
public int createOperand (String iString, char oprtr,int startPos); 
^
Calculator.java:129: cannot find symbol
symbol : variable startpos
location: class Calculator.AddHandler
int indx=startpos+1;
^
Calculator.java:129: incompatible types
found : <nulltype>
required: int
int indx=startpos+1;
^
Calculator.java:132: package i does not exist
while ((i.String.valueOf(indx) != oprtr) || (indx < (String.length() -1)))
^
Calculator.java:132: cannot find symbol
symbol : variable oprtr
location: class Calculator.AddHandler
while ((i.String.valueOf(indx) != oprtr) || (indx < (String.length() -1)))
^
Calculator.java:132: non-static method length() cannot be referenced from a static context
while ((i.String.valueOf(indx) != oprtr) || (indx < (String.length() -1)))
^
Calculator.java:133: cannot find symbol
symbol : variable iString
location: class Calculator.AddHandler
if ((iString.charAt(indx) >="0") || (iString.charAt(indx) <= "9"))
^
Calculator.java:133: cannot find symbol
symbol : variable iString
location: class Calculator.AddHandler
if ((iString.charAt(indx) >="0") || (iString.charAt(indx) <= "9"))
^
Calculator.java:134: cannot find symbol
symbol : variable iString
location: class Calculator.AddHandler
operand = (operand *10) + iString.charAt(indx++).parseInt();
^
Calculator.java:134: operator + cannot be applied to int,iString.charAt.parseInt
operand = (operand *10) + iString.charAt(indx++).parseInt();
^
Calculator.java:134: incompatible types
found : <nulltype>
required: int
operand = (operand *10) + iString.charAt(indx++).parseInt();
^
Calculator.java:136: return outside method
return operand;
^
21 errors

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

the program is as follows

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

/* Create a window that looks like the pad of a calculator
Make it work just like a calculator...this calculator will only 
work w/ integer operations
*/
public class Calculator extends JFrame
{
/* CONSTANTS */
private final String buttons[] = {"7","8","9","/",
"4","5","6","*",
"1","'2","3","-",
"0",".","=","+" };

/* VARIABLES */

private JPanel textPanel = new JPanel();
private JPanel nmbrPadPanel = new JPanel();
private JPanel clearBttnPanel= new JPanel();

private JTextField nmbrDisplay = new JTextField ("0",25);

private GridLayout calcPadLayout;
private JButton calcButton[];

private JButton clearBttn = new JButton ("Clear");

public Calculator()
{
super ("Calculator");
setLayout (new BorderLayout());

textPanel.add(nmbrDisplay);
add (textPanel, BorderLayout.NORTH);

nmbrPadPanel.setLayout(new GridLayout(buttons.length/4,buttons.length/4));
calcButton = new JButton[buttons.length];
for (int indx = 0; indx < buttons.length; indx++)
{
calcButton [indx] = new JButton (buttons[indx]);
nmbrPadPanel.add (calcButton[indx]);
calcButton[indx].addActionListener(new AddHandler()); 
} //end for

add (nmbrPadPanel, BorderLayout.CENTER);

clearBttnPanel.add(clearBttn);
clearBttn.addActionListener (new ClearButtonHandler());

add (clearBttnPanel, BorderLayout.SOUTH);

}	//end constructor Calculator

class ClearButtonHandler implements ActionListener
{ 
public void actionPerformed (ActionEvent avt)
{
nmbrDisplay.setText("0"); 
} 
} // end action handler ClearButtonHandler

class AddHandler implements ActionListener
{ 
public void actionPerformed (ActionEvent evt)
{
char iChar, operator;
String iString = "";
String inChar;
double answer;

inChar = evt.getActionCommand();
iString += inChar;

iChar = inChar.charAt(0);
switch (iChar)
{ 
case '=' : answer = ParseAndPerformAction(iString,operator);
nmbrDisplay.setText("" + answer);
break;

case '*' : 
case '-' : 
case '+' :
case '/' : operator = iChar;
break; 
} // end switch
} //end actionPerformed

/* 
create the operand and perform the indicated operation
*/	
public double ParseAndPerformAction (String cString, char operator); 
{ 
int operand1=0,
operand2=0;

double	answer = 0;

operand1 = createOperand (cString, operator,-1); 
operand2 = createOperand (cString, ichar,cString.indexOf(operator));

switch (operator)
{ 
case '+': answer = operand1 + operand2;
break; 
case '-' : answer = operand1 - operand2;
break;

case '*': answer = operand1 * operand2;
break; 
case '/': //try{
answer = operand1 / operand2;
// catch divideByZeroException 
// {
// }
// } // end try

break; 
}// end switch

return answer;

} // end method ParseandPerformAction

public int createOperand (String iString, char oprtr,int startPos); 
{ 
int indx=startpos+1;
int operand = 0;

while ((i.String.valueOf(indx) != oprtr) || (indx < (String.length() -1)))
if ((iString.charAt(indx) >="0") || (iString.charAt(indx) <= "9"))
operand = (operand *10) + iString.charAt(indx++).parseInt();

return operand;

} // method createOperand

} // end class AddHandler

} // end class Calculator


----------

