I'm making a calculator program but I'm stuck somewhere. I'm still new to J2ME. So guys, please take it easy on me. :). I've been trying to find the error for 4 hours now. I think the problem is, x won't retain the value..
anyway, here's the full code.
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemCommandListener;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.*;
public class calcu extends MIDlet implements ItemCommandListener{
private static final int numberMax = 10;
private static final int operMax = 5;
Display screen;
Form container;
Command calculate;
Command numb;
Command exit;
StringItem number[] = new StringItem[numberMax];
StringItem operation[] = new StringItem[operMax];
TextField output;
char calc;
double x;
double y;
public calcu(){
container = new Form("Calculator");
output = new TextField("", "", 50, TextField.NUMERIC);
exit = new Command("exit", Command.EXIT, 1);
calculate = new Command("", Command.OK, 1);
numb = new Command("", Command.OK, 1);
container.append(output);
for(int i = 0; i < numberMax; i++){
number[i] = new StringItem("", " " + i + " ", StringItem.BUTTON);
number[i].setDefaultCommand(numb);
number[i].setItemCommandListener(this);
}
for(int i = 0; i < operMax; i++){
String str;
switch(i){
case 0: str = "+"; break;
case 1: str = "-"; break;
case 2: str = "*"; break;
case 3: str = "/"; break;
case 4: str = "="; break;
default: str = "$"; break;
}
operation[i] = new StringItem("", " " + str + " ", StringItem.BUTTON);
operation[i].setDefaultCommand(calculate);
operation[i].setItemCommandListener(this);
}
arrange();
container.addCommand(exit);
}
public void arrange(){
container.append(number[7]);
container.append(number[8]);
container.append(number[9]);
container.append(number[4]);
container.append(number[5]);
container.append(number[6]);
container.append(number[1]);
container.append(number[2]);
container.append(number[3]);
container.append(operation[0]);
container.append(number[0]);
container.append(operation[1]);
container.append(operation[2]);
container.append(operation[4]);
container.append(operation[3]);
}
public void startApp() {
screen = Display.getDisplay(this);
screen.setCurrent(container);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Item item) {
if(c == exit){
this.notifyDestroyed();
}
if(item == number[0])
output.setString(output.getString() + "0");
else if(item == number[1])
output.setString(output.getString() + "1");
else if(item == number[2])
output.setString(output.getString() + "2");
else if(item == number[3])
output.setString(output.getString() + "3");
else if(item == number[4])
output.setString(output.getString() + "4");
else if(item == number[5])
output.setString(output.getString() + "5");
else if(item == number[6])
output.setString(output.getString() + "6");
else if(item == number[7])
output.setString(output.getString() + "7");
else if(item == number[8])
output.setString(output.getString() + "8");
else if(item == number[9])
output.setString(output.getString() + "9");
if(c == calculate)
{
if(item == operation[0])
{
calc = '+';
x = Double.parseDouble(output.getString());
output.setString("");
}
else if(item == operation[4])
{
y = Double.parseDouble(output.getString());
x += y;
output.setString("" + x);
}
}
}
}
As you can see, the program is not finished yet. I'm still testing. The problem is in the last part but I'm not sure. Please help guys.