You need to sign in to do that
Don't have an account?
Naresh Kumar Mohan Thirukkovaluru
implementing calculator ....but giving error not taking values thru command buttons.............error invalid integer
<apex:page controller="calc" >
<apex:form >
<apex:pageBlock >
<apex:inputText maxlength="10" value="{!result}" size="12"/>
<apex:panelGrid title="Calculator" cellpadding="2" columns="4" border="5" bgcolor="gold">
<apex:commandButton value="9" action="{!nine}" />
<apex:commandButton value="8" action="{!eight}"/>
<apex:commandButton value="7" action="{!seven}"/>
<apex:commandButton value="/" action="{!divide}"/>
<apex:commandButton value="6" action="{!six}"/>
<apex:commandButton value="5" action="{!five}"/>
<apex:commandButton value="4" action="{!four}"/>
<apex:commandButton value="*" action="{!multiply}"/>
<apex:commandButton value="3" action="{!three}"/>
<apex:commandButton value="2" action="{!two}"/>
<apex:commandButton value="1" action="{!one}"/>
<apex:commandButton value="+" action="{!add}"/>
<apex:commandButton value="0" action="{!zero}"/>
<apex:commandButton value="."/>
<apex:commandButton value="-" action="{!minus}"/>
<apex:commandButton value="=" action="{!equal}"/>
</apex:panelGrid>
</apex:pageBlock>
</apex:form>
</apex:page>
public with sharing class calc {
public PageReference equal() {
result=string.valueOf(num);
return null;
}
public String result { get; set; }
integer num=0;
public PageReference minus() {
num-=integer.valueOf(result);
result=null;
return null;
}
public PageReference zero() {
if(integer.valueOf(result)==num){
result = '0';
return null;
}
else{
result+= '0';
return null;
}
}
public PageReference add() {
num+=integer.valueOf(result);
result=null;
return null;
}
public PageReference one() {
if(integer.valueOf(result)==num){
result = '1';
return null;
}
else{
result+='1';
return null;
}
}
public PageReference two() {
if(integer.valueOf(result)==num){
result = '2';
return null;
}
else{
result+='2';
return null;
}
}
public PageReference three() {
if(integer.valueOf(result)==num){
result = '3';
return null;
}
else{
result+='3';
return null;
}
}
public PageReference multiply() {
num=num*integer.valueOf(result);
result=null;
return null;
}
public PageReference four() {
if(integer.valueOf(result)==num){
result = '4';
return null;
}
else{
result+='4';
return null;
}
}
public PageReference five() {
if(integer.valueOf(result)==num){
result = '5';
return null;
}
else{
result+='5';
return null;
}
}
public PageReference six() {
if(integer.valueOf(result)==num){
result = '6';
return null;
}
else{
result+='6';
return null;
}
}
public PageReference divide() {
num=num/integer.valueOf(result);
result=null;
return null;
}
public PageReference seven() {
if(integer.valueOf(result)==num){
result = '7';
return null;
}
else{
result+='7';
return null;
}
}
public PageReference eight() {
if(integer.valueOf(result)==num){
result = '8';
return null;
}
else{
result+='8';
return null;
}
}
public PageReference nine() {
if(integer.valueOf(result)==num){
result = '9';
return null;
}
else{
result+='9';
return null;
}
}
}
<apex:form >
<apex:pageBlock >
<apex:inputText maxlength="10" value="{!result}" size="12"/>
<apex:panelGrid title="Calculator" cellpadding="2" columns="4" border="5" bgcolor="gold">
<apex:commandButton value="9" action="{!nine}" />
<apex:commandButton value="8" action="{!eight}"/>
<apex:commandButton value="7" action="{!seven}"/>
<apex:commandButton value="/" action="{!divide}"/>
<apex:commandButton value="6" action="{!six}"/>
<apex:commandButton value="5" action="{!five}"/>
<apex:commandButton value="4" action="{!four}"/>
<apex:commandButton value="*" action="{!multiply}"/>
<apex:commandButton value="3" action="{!three}"/>
<apex:commandButton value="2" action="{!two}"/>
<apex:commandButton value="1" action="{!one}"/>
<apex:commandButton value="+" action="{!add}"/>
<apex:commandButton value="0" action="{!zero}"/>
<apex:commandButton value="."/>
<apex:commandButton value="-" action="{!minus}"/>
<apex:commandButton value="=" action="{!equal}"/>
</apex:panelGrid>
</apex:pageBlock>
</apex:form>
</apex:page>
public with sharing class calc {
public PageReference equal() {
result=string.valueOf(num);
return null;
}
public String result { get; set; }
integer num=0;
public PageReference minus() {
num-=integer.valueOf(result);
result=null;
return null;
}
public PageReference zero() {
if(integer.valueOf(result)==num){
result = '0';
return null;
}
else{
result+= '0';
return null;
}
}
public PageReference add() {
num+=integer.valueOf(result);
result=null;
return null;
}
public PageReference one() {
if(integer.valueOf(result)==num){
result = '1';
return null;
}
else{
result+='1';
return null;
}
}
public PageReference two() {
if(integer.valueOf(result)==num){
result = '2';
return null;
}
else{
result+='2';
return null;
}
}
public PageReference three() {
if(integer.valueOf(result)==num){
result = '3';
return null;
}
else{
result+='3';
return null;
}
}
public PageReference multiply() {
num=num*integer.valueOf(result);
result=null;
return null;
}
public PageReference four() {
if(integer.valueOf(result)==num){
result = '4';
return null;
}
else{
result+='4';
return null;
}
}
public PageReference five() {
if(integer.valueOf(result)==num){
result = '5';
return null;
}
else{
result+='5';
return null;
}
}
public PageReference six() {
if(integer.valueOf(result)==num){
result = '6';
return null;
}
else{
result+='6';
return null;
}
}
public PageReference divide() {
num=num/integer.valueOf(result);
result=null;
return null;
}
public PageReference seven() {
if(integer.valueOf(result)==num){
result = '7';
return null;
}
else{
result+='7';
return null;
}
}
public PageReference eight() {
if(integer.valueOf(result)==num){
result = '8';
return null;
}
else{
result+='8';
return null;
}
}
public PageReference nine() {
if(integer.valueOf(result)==num){
result = '9';
return null;
}
else{
result+='9';
return null;
}
}
}
Please go through the following thread which have sample codes to create a calculator:
http://www.infallibletechie.com/2013/01/sample-caluculator-using-apex-in.html
https://developer.salesforce.com/forums/ForumsMain?id=906F000000090rEIAQ