DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2
  1. #1
    atif Guest

    validating input in java??

    i have a problem in java...and i'm not quite sure how to solve it,i need
    help in validating the input as stated in the problem below everything else
    i can do..

    i have started something but not sure what to do next.

    Here is the problem:
    Problem 1
    A program accepts identification number, first name, last name, and email
    address of a user. It validates the data as per following specifications:

    .identification number – no blanks, four numerical characters;
    .first name – no blanks, all alphabetical characters;
    .last name – same as for first name;
    .email address–
    .no blanks;
    .only one ‘@’character;
    ,there is at least one word before ‘@’, and at least two words after ‘@’,
    both words(following ‘@’ joined by ‘.’, no space between the words before
    and after the ‘.’ character.
    Assume that there are maximum of two words following ‘@’character, and there
    are maximum of 25 characters in the email address.

    If any of the data item is invalid, program displays an appropriate message
    to the user and asks for re-entry of that item.
    When all data items entered are valid, program creates a password for the
    user by joining:
    .first two characters of first name;
    .2nd and 3rd characters of identification
    .last three characters from the last name;
    in above order.
    Password is then relayed back to user. Later then enters this password and
    program then checks that user has entered the correct password (as created
    for him/her), and displays the message to the user accordingly.

    You do not need to create any special user interface but must utilize string
    and array processing functions

    and here is my source code so far..


    kind regards,


    import java.io.*;
    public class UserValidation {

    //declare variables
    private int id = 0;
    private String firstname = "";
    private String lastname = "";
    private String email = "";

    private BufferedReader input = new BufferedReader (new InputStreamReader(System.in));

    public UserValidation(int id, String firstname, String lastname, String
    this.id = id;
    this.firstname = firstname;
    this.lastname = lastname;
    this.email = email;

    //you can throw NumberFormatException to verify if the user entered an
    // parseInt throws a NumberFormatException if the argument is not valid

    public int getId() throws NumberFormatException{
    //System.out.println("Input UserID:");
    //id = Integer.parseInt(input.readLine());
    return id;
    public String getfName(){
    return firstname;
    public String getlName(){
    return lastname;
    public String getEmail(){
    return email;
    public void display(){

    // you must catch the IOException thrown by getId function

    System.out.println(" " + getId() + " " + getfName() + " " + getlName()
    + " " + getEmail());
    }catch(NumberFormatException nfe){}

    public static void main (String args[]){
    //initialize user data members with an appropriate constructor
    or set functions
    UserValidation user = new UserValidation(1234,"Atif","Bokhari","Atif_bok@yahoo.com");

  2. #2
    MarkN Guest

    Re: validating input in java??

    Without doing your home work for you ...

    Create a different class to be the "user interface". If you are doing command
    line (and you are) you will probably need a loop.

    Create a User class with attributes and getters and setters for each attribute.
    Each attribute can be its basic type (String name or a special class (Name
    name. In the setter or special class when the setter is set call validation
    methods. If anything is wrong notify listeners or if everything is right
    let them know.

    Something like ..
    Check if each attribute is valid.
    If any is invalid, stop and inform.
    If all are valid, inform.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.