Hello everyone.

Im trying to write a program for a cellphone that gets inputs from the user, does some calculations then displays various outputs.

I am very new to java programming and getting a little confused. I am having trouble with finding out how to read the values from the text fields to use them for the calculations.I have looked on the web but havent managed to find anything helpful.Any help will be greatly appreciated. Thanks in advance.

Here is my code. Im sure there are lots of mistakes

import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.Spacer;
import javax.microedition.lcdui.ImageItem;
import javax.microedition.lcdui.TextField;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.ChoiceGroup;


import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Command;
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.ItemStateListener;



public class FormExample extends MIDlet implements CommandListener,ItemStateListener {

    private Form form;
    private Spacer spacer;
    private ImageItem imageItem;
    private TextField lamps,type,txtField,calc;

    private StringItem stringItem,calcu;
    private ChoiceGroup choiceGroup;
                public int result,result2,result3;
                public int number,lamp,nolamp,hour;


    public FormExample() {
        form = new Form("Prog1");


        stringItem = new StringItem
                                ("electricity ", "Saving");
        form.append(stringItem);


        lamps = new TextField(
                                "Number of lamps: ","", 50, TextField.NUMERIC);
        form.append(lamps);

        // text field for lamp type
        type = new TextField(
        "Lamp Type: ", "", 50, TextField.NUMERIC);
        form.append(type);


                // calculation 1
                number = lamp*nolamp;


        // similar to using a List
        choiceGroup = new ChoiceGroup(
        "Cost per KiloWatt hour: ",
        Choice.EXCLUSIVE,
        new String[] {"Commercial Low Load = 22c", "Commercial High Load = 37c","Residential = 58c","Commercial = 59c"},null);

                 form.append(choiceGroup);
                 form.setItemStateListener (this);

                txtField = new TextField
                ("Number of hours: ","",50,TextField.NUMERIC);
                form.append(txtField);



                //calculation 2
                result2 = result*hour;

        // put some space between the items to segregate
        spacer = new Spacer(20, 20);
        form.append(spacer);

                //display the result here
                calcu = new StringItem
               ("Calculation", String.valueOf(result2));
    form.append(calcu);


        // an image may not be found,
        // therefore the Exception must be handled
        // or ignored
        try {
            imageItem = new ImageItem(
                "Developed By: ",
                Image.createImage
                                                                ("/logo.png"),
                ImageItem.LAYOUT_DEFAULT,
                " ");
            form.append(imageItem);
        } catch(Exception e) {}




        // adding commands

        form.addCommand(
        new Command("EXIT",Command.EXIT, 2));
        form.addCommand(
        new Command("OK", Command.OK, 1));

          form.setCommandListener(this);
        }

    // handle commands
    public void commandAction(Command com, Displayable dis)
        {

        String label = com.getLabel();

        if("EXIT".equals(label))
          notifyDestroyed();
        else if("OK".equals(label))
          processForm();
        }


    public void processForm() {

        // process Form

    }


    public void startApp() {
        Display display = Display.getDisplay(this);
        display.setCurrent(form);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

// handles the choice from the list selection

    public void itemStateChanged(Item item) {
        if(item == choiceGroup){
            int index = choiceGroup.getSelectedIndex();
            switch(index)
            {
                case 0: number += 22;
                        result += number/100;
                    break;
                case 1: number += 23;
                        result += number/100;
                    break;
                case 2: number += 56;
                        result += number/100;
                    break;
                case 3: number += 58;
                        result += number/100;
                    break;
            }
        }
    }

}

Exactly same way as you will do it with any other component for user data collection in Java, getString() (do not forget to parse String to numerical values)

PS: In the future please use code tags to post any code as following [code] YOUR CODE HERE [/code]

Awesome thank you for the help. My application is working hundreds now.

I shall do that for future posts, thnx again

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.