954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

midlet cellphone application help

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;
}
}
}

}

wrathness
Newbie Poster
2 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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]

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

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

I shall do that for future posts, thnx again

wrathness
Newbie Poster
2 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You