I know I've posted this already but I posted it in the wrong forum.. anyway..

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.

x is supposed to get the value from the textbox. For example, user inputs 39. When he press the '+' button, x is supposed to get the value 39 and store it then the program erases the value 39 in the textbox. x is a double. As you can see, i've parsed the value from the textbox.
The program will know the user is asking for an addition. So when he inputs another number, say 12, then press '=' button, y is supposed to get the value; convert the string to double then add and store the sum to x and display it.
Somehow, the program will display the last inputted value. In this case, 12. It's suppose to display 51.

I think your answer may lie in a classic error in Java!

At line 127 you test if the command is 'calculate' and then test for which operation is selected. Java will test to see of the object 'c' is the same as the object 'calculate' - which it is not so nothing will happen.

You need to test for equality o the values, usually with some form of equals command like:

if (c.equals(calculate))

you will need to check the precise syntax.

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.