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.

I think the problem is, x won't retain the value..

What do you mean by "won't retain"? Is some code changing the value from the one you give it to another value?
What value do you give it?
What value is it changed to?

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.

the program will display the last inputted value. In this case, 12. It's suppose to display 51.

Try adding some printlns to your code to show the values of all the variables as their values are changed. If you print them out as the code executes and after every statement where the value of the variables is changed, you should see where the problem is.

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.