Using forms and Commands
Written by: maffelu , 2009-06-17 18:37:08
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class FormMIDlet extends MIDlet implements CommandListener
private Command exitCommand = new Command("Exit", Command.EXIT, 1);
private Command reverseCommand = new Command("Reverse letters", Command.SCREEN, 1);
private Command asciiValueCommand = new Command("Letter ASCII value", Command.SCREEN, 1);
private Command countCommand = new Command("Count letters", Command.SCREEN, 1);
public class FormMIDlet extends MIDlet implements CommandListener{
private Display display;
private Command exitCommand = new Command("Exit", Command.EXIT, 1);
private Command reverseCommand = new Command("Reverse letters", Command.SCREEN, 1);
private Command asciiValueCommand = new Command("Letter ASCII value", Command.SCREEN, 1);
private Command countCommand = new Command("Count letters", Command.SCREEN, 1);
private Form form;
private TextField txtInput;
private StringItem strOutput;
private TextHandler textHandler;
public FormMIDlet(){
textHandler = new TextHandler();
txtInput = new TextField("Write anything", "", 25, TextField.ANY);
strOutput = new StringItem("Output: ", "");
form = new Form("The magic box!");
form.append(txtInput);
form.append(strOutput);
form.addCommand(exitCommand);
form.addCommand(reverseCommand);
form.addCommand(asciiValueCommand);
form.addCommand(countCommand);
form.setCommandListener(this);
}
public void commandAction(Command c, Displayable d){
}
public void exitMIDlet(){
destroyApp(true);
notifyDestroyed();
}
public void commandAction(Command c, Displayable d){
if(c == exitCommand){
this.exitMIDlet();
}
}
public class TextHandler {
/** * A method to reverse the order of the characters in a string * * @param s Any string * @return The string in reverse order */
public String getReverseLetters(String s){
String reversed = "";
for(int i = s.length() - 1; i >= 0; i--){
reversed += String.valueOf(s.charAt(i));
}
return reversed;
}
/** * A method to get the sum of the characters ASCII values * * @param s Any string * @return An int representing the ASCII value of the string */
public int getASCIIValue(String s){
int sum = 0;
for(int i = 0; i < s.length(); i++){
sum += s.charAt(i);
}
return sum;
}
/** * A method to get the amount of characters in a string * * @param s Any string * @return An int representing the number of characters in the string */
public int getLetterCount(String s){
return s.length();
}
}
public void commandAction(Command c, Displayable d){
if(c == exitCommand){
this.exitMIDlet();
}
else if(c == reverseCommand){
String input = txtInput.getString();
String reversedInput = textHandler.getReverseLetters(input);
strOutput.setText(reversedInput);
}
else if(c == asciiValueCommand){
String input = txtInput.getString();
int asciiValue = textHandler.getASCIIValue(input);
strOutput.setText(Integer.toString(asciiValue));
}
else if(c == countCommand){
String input = txtInput.getString();
int sum = textHandler.getLetterCount(input);
strOutput.setText(Integer.toString(sum));
}
}
/* * To change this template, choose Tools | Templates * and open the template in the editor. */
package org.MorkaLork.FormsExample;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/** * @author maffelu */
public class FormMIDlet extends MIDlet implements CommandListener{
private Display display;
private Command exitCommand = new Command("Exit", Command.EXIT, 1);
private Command reverseCommand = new Command("Reverse letters", Command.SCREEN, 1);
private Command asciiValueCommand = new Command("Letter ASCII value", Command.SCREEN, 1);
private Command countCommand = new Command("Count letters", Command.SCREEN, 1);
private Form form;
private TextField txtInput;
private StringItem strOutput;
private TextHandler textHandler;
public FormMIDlet(){
textHandler = new TextHandler();
txtInput = new TextField("Write anything", "", 25, TextField.ANY);
strOutput = new StringItem("Output: ", "");
form = new Form("The magic box!");
form.append(txtInput);
form.append(strOutput);
form.addCommand(exitCommand);
form.addCommand(reverseCommand);
form.addCommand(asciiValueCommand);
form.addCommand(countCommand);
form.setCommandListener(this);
}
public void initMIDlet(){
display = Display.getDisplay(this);
display.setCurrent(form);
}
public void exitMIDlet(){
destroyApp(true);
notifyDestroyed();
}
public void startApp() {
if(display == null){
initMIDlet();
}
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable d){
if(c == exitCommand){
this.exitMIDlet();
}
else if(c == reverseCommand){
String input = txtInput.getString();
String reversedInput = textHandler.getReverseLetters(input);
strOutput.setText(reversedInput);
}
else if(c == asciiValueCommand){
String input = txtInput.getString();
int asciiValue = textHandler.getASCIIValue(input);
strOutput.setText(Integer.toString(asciiValue));
}
else if(c == countCommand){
String input = txtInput.getString();
int sum = textHandler.getLetterCount(input);
strOutput.setText(Integer.toString(sum));
}
}
}
package org.MorkaLork.FormsExample;
/** * * @author maffelu */
public class TextHandler {
/** * A method to reverse the order of the characters in a string * * @param s Any string * @return The string in reverse order */
public String getReverseLetters(String s){
String reversed = "";
for(int i = s.length() - 1; i >= 0; i--){
reversed += String.valueOf(s.charAt(i));
}
return reversed;
}
/** * A method to get the sum of the characters ASCII values * * @param s Any string * @return An int representing the ASCII value of the string */
public int getASCIIValue(String s){
int sum = 0;
for(int i = 0; i < s.length(); i++){
sum += s.charAt(i);
}
return sum;
}
/** * A method to get the amount of characters in a string * * @param s Any string * @return An int representing the number of characters in the string */
public int getLetterCount(String s){
return s.length();
}
}
There are 3 comments on this article.
maffelu
2010-05-27 22:03:18
What do you mean with The Code Of Consonants?
irvine chan
2010-09-29 19:43:24
can u write a the full code for ascii converter?
If you have any question or just want to leave a message, just fill out the form below!
Your e-mail will not be visible in your post, it is for validation reasons only
Maffelu
Creator and admin of MorkaLork.com.
Started programming in HTML back when frames and tables was the way to design a page, moved on to Pascal/Delphi, PHP, javascript/jQuery, VB.NET/C#, Java and C++.
Currently studies .NET (in general) focusing on ASP.NET.
Nazymounting vo
2010-05-27 03:32:23
can u write the code of consonants