Search code examples
mobilejava-memidplcdui

parsing int or double from text field in J2ME


I have two text fields in Java ME and I'm trying to create a third field will calculate a total of the two previous text fields that the user would input. I know I would first have convert the first two text fields into Int values. And then set them to variables to add and display in the third text field.

But with the code that I'm working with below I get error messages, Is there another way of doing this?

public firstMIDlet()
{
    d = Display.getDisplay(this);

    muaj1 = new TextField("0-11 muaj", "", 6, TextField.ANY);
    int m1Int = Integer.parseInt(TextField.getString());

    muaj2 = new TextField("12-23 muaj", "", 6, TextField.ANY);
    int m2Int = Integer.parseInt(TextField.getString());

    totali = new TextField("TOTALI", "", 7, TextField.ANY);
}

Solution

  • Thanks guys,

    I have gotten the calculation to work, by using TextField.NUMERIC and creating a new function for calculation

     public void calc() {
        int a1=0, a2=0, a3=0, a4=0, res=0;
        a1 = Integer.parseInt(muaj1.getString());
        a2 = Integer.parseInt(muaj2.getString());
        a3 = Integer.parseInt(muaj3.getString());
        a4 = Integer.parseInt(muaj4.getString());
        totali.setText((a1 + a2 + a3 + a4) + "");
       }