This discussion is archived
1 Reply Latest reply: Apr 21, 2011 12:45 AM by Anshul RSS

Java ME, Input Validation

845988 Newbie
Currently Being Moderated
I am fairly new to Java ME and I am having some trouble, I have an application that allows you to rate food establishment, however I am trying to add some validation so that if the user leaves a field blank, when they try to submit the details they have entered it will stop them from continuing and make them go back. Here is the code I have so far would anyone please be able to help me?


package hello;

import javax.microedition.midlet.*;import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
import javax.microedition.io.*;


public class Rater extends MIDlet implements CommandListener
{
private Display mDisplay;
private Alert mAlert;
private Form mForm;
private ChoiceGroup chcEstablishmentType;
private TextField ID, txtPlaceName, txtLocation, txtRating, txtFoodsold;
private String strPlace, strLocation, strID, strRating, strFoodsold, strEstablishmentType;

public Rater()
{
String [] EstablishmentType = {"Bar", "Restaurant ", "Cafe'"};
mForm = new Form("Enter your order details");
ID = new TextField("ID*: ", null, 4, TextField.ANY);
chcEstablishmentType = new ChoiceGroup("Type of Establishment*", Choice.EXCLUSIVE, EstablishmentType, null);
txtFoodsold = new TextField("Food Sold*", null, 20, TextField.ANY);
txtPlaceName = new TextField("Establishment Name*:", null, 15, TextField.ANY);
txtLocation = new TextField("Location*:", null, 15, TextField.ANY);
txtRating = new TextField("Rating*:", null, 5, TextField.ANY);
mForm.append(ID);
mForm.append(txtPlaceName);
mForm.append(txtLocation);
mForm.append(txtFoodsold);
mForm.append(txtRating);
mForm.append(chcEstablishmentType);
mForm.addCommand(new Command("Exit", Command.EXIT, 0));
mForm.addCommand(new Command("Next", Command.SCREEN, 0));
mForm.setCommandListener(this);
mAlert = new Alert("Your order", "", null, AlertType.CONFIRMATION);
mAlert.addCommand(new Command("Back", Command.BACK, 0));
mAlert.addCommand(new Command("Confirm", Command.OK,1));
mAlert.setTimeout(Alert.FOREVER);
}

protected void startApp()
{
mDisplay = Display.getDisplay(this);
mDisplay.setCurrent(mForm);
}


public void commandAction(Command c, Displayable d)
{
if (c.getLabel().equals("Exit"))
{
notifyDestroyed();
}
if (c.getLabel().equals("Next"))
{
strID = ID.getString();
strPlace = txtPlaceName.getString();
strLocation = txtLocation.getString();
strRating = txtRating.getString();
strFoodsold = txtFoodsold.getString();


strEstablishmentType = chcEstablishmentType.getString(chcEstablishmentType.getSelectedIndex());
String strOrderDetails = "ID: " + strID + "ordered " + strFoodsold + " from a \n " + strEstablishmentType + " called " + strPlace + "\n in " + strLocation
+ "\n rating it with " + strRating + " stars ";
mAlert.setString(strOrderDetails);
mDisplay.setCurrent(mAlert);
}

}

protected void destroyApp(boolean param) {}
protected void pauseApp() {}
}
  • 1. Re: Java ME, Input Validation
    Anshul Explorer
    Currently Being Moderated
    hey try this code:-

    import javax.microedition.lcdui.;*
    import javax.microedition.midlet.MIDlet;
    public class Rater extends MIDlet implements CommandListener
    *{*
    private Display mDisplay;
    private Alert mAlert;
    private Form mForm;
    private ChoiceGroup chcEstablishmentType;
    private TextField ID, txtPlaceName, txtLocation, txtRating, txtFoodsold;
    private String strPlace, strLocation, strID, strRating, strFoodsold, strEstablishmentType;
    private Alert error_alert;

    public Rater()
    *{*
    String [] EstablishmentType = {"Bar", "Restaurant ", "Cafe'"};
    mForm = new Form("Enter your order details");
    ID = new TextField("ID: ", null, 4, TextField.ANY);*
    *chcEstablishmentType = new ChoiceGroup("Type of Establishment*", Choice.EXCLUSIVE, EstablishmentType, null);*
    *txtFoodsold = new TextField("Food Sold*", null, 20, TextField.ANY);*
    *txtPlaceName = new TextField("Establishment Name*:", null, 15, TextField.ANY);*
    *txtLocation = new TextField("Location*:", null, 15, TextField.ANY);*
    *txtRating = new TextField("Rating*:", null, 5, TextField.ANY);*
    *mForm.append(ID);*
    *mForm.append(txtPlaceName);*
    *mForm.append(txtLocation);*
    *mForm.append(txtFoodsold);*
    *mForm.append(txtRating);*
    *mForm.append(chcEstablishmentType);*
    *mForm.addCommand(new Command("Exit", Command.EXIT, 0));*
    *mForm.addCommand(new Command("Next", Command.SCREEN, 0));*
    *mForm.setCommandListener(this);*
    *mAlert = new Alert("Your order", "", null, AlertType.CONFIRMATION);*
    *mAlert.addCommand(new Command("Back", Command.BACK, 0));*
    *mAlert.addCommand(new Command("Confirm", Command.OK,1));*
    *mAlert.setTimeout(Alert.FOREVER);*
    *error_alert = new Alert("error","",null,AlertType.ERROR);*
    *error_alert.setTimeout(500);*
    *}*

    *protected void startApp()*
    *{*
    *mDisplay = Display.getDisplay(this);*
    *mDisplay.setCurrent(mForm);*
    *}*
    *public void commandAction(Command c, Displayable d)*
    *{*
    *if (c.getLabel().equals("Exit"))*
    *{*
    *notifyDestroyed();*
    *}*
    *if (c.getLabel().equals("Next"))*
    *{*
    *strID = ID.getString();*
    *strPlace = txtPlaceName.getString();*
    *strLocation = txtLocation.getString();*
    *strRating = txtRating.getString();*
    *strFoodsold = txtFoodsold.getString();*
    *strEstablishmentType = chcEstablishmentType.getString(chcEstablishmentType.getSelectedIndex());*

    *if(strID.equals(""))*
    *{*
    *String s = "enter ID";*
    *error_alert.setString(s);*
    *mDisplay.setCurrent((Displayable) error_alert);*
    *}*
    *if(strPlace.equals(""))*
    *{*
    *String s = "enter establishment name";*
    *error_alert.setString(s);*
    *mDisplay.setCurrent((Displayable) error_alert);*
    *}*

    *else if(strLocation.equals(""))*
    *{*
    *String s = "enter Location";*
    *error_alert.setString(s);*
    *mDisplay.setCurrent((Displayable) error_alert);*
    *}*

    *else if (strRating.equals(""))*
    *{*
    *String s = "enter rating";*
    *error_alert.setString(s);*
    *mDisplay.setCurrent((Displayable) error_alert);*
    *}*

    *else if(strFoodsold.equals(""))*
    *{*
    *String s = "enter the name of food sold";*
    *error_alert.setString(s);*
    *mDisplay.setCurrent((Displayable) error_alert);*
    *}*

    *else if(strEstablishmentType.equals(""))*
    *{*
    *String s = "enter type of establishment";*
    *error_alert.setString(s);*
    *mDisplay.setCurrent((Displayable) error_alert);*
    *}*


    *else*
    *{*
    *String strOrderDetails = "ID: " + strID + "ordered " + strFoodsold + " from a \n " + strEstablishmentType + " called " + strPlace + "\n in " + strLocation*
    *+ "\n rating it with " + strRating + " stars ";*
    *mAlert.setString(strOrderDetails);*
    *mDisplay.setCurrent(mAlert);*
    *}*
    *}*

    *}*

    *protected void destroyApp(boolean param) {}*
    *protected void pauseApp() {}*
    *}*

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points