Question

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);
}
Was it helpful?

Solution 3

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) + "");
   }

OTHER TIPS

You should post what errors you get and where. So probably...

If the value returned is "" (which is your case), than it cannot convert it to Integer and returns NumberFormatException.

You should use TextField.NUMERIC if you want to parse it as Integer.

Also you might need to start a thread which calculates the value of third field dynamically.

And you will get error whenever you call TextField.getString() because it is not a static method, and it cannot know from which TextField it should get it.

so, you need to do something like this:

    private Form f;
    public firstMIDlet() {
        d = Display.getDisplay(this);
        f = new Form("myform);
        muaj1 = new TextField("0-11 muaj","",6,TextField.NUMERIC);
        muaj2 = new TextField("12-23 muaj","",6,TextField.NUMERIC);
        totali = new TextField("TOTALI","",7,TextField.NUMERIC | TextField.UNEDITABLE);
        myform.append(muaj1);
        myform.append(muaj2);
        myform.append(totali);
        startUpdating();
    }

    public void startApp() {
        d.setCurrent(form);
    }

    public void startUpdating() {
        new Thread () {
            public void run() {
                try {
                    int m1Int = Integer.parseInt(muaj1.getString());
                    int m2Int = Integer.parseInt(muaj2.getString());
                    totali.setString((m1Int + m2Int) + "");
                }
                catch (NumberFormatExeption nfe) {}
            }
        }.start();
    }

where

    "" + someInt

will convert to String. Also you may use

    Integer.toString(m1Int + m2Int)

Since you need integers in the textField so instead of TextField.ANY use TextField.NUMERIC in all the three textFields. This will restrict user to enter numbers only. However you can add constraints to the third textField totali.setConstraints(TextField.UNEDITABLE);

Moreover read documentation to familiarize yourself with j2me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top