Results 1 to 2 of 2
  1. #1
    Gtrain is offline Member
    Join Date
    Aug 2009
    Posts
    1
    Rep Power
    0

    Default Code streamlining

    Hey Guys,
    I am new to java and new to the board, so firstly hello!

    Secondly......

    I am working on a project part of this is to validate a student id that needs to be of length 8 and the first seven chars will be digits and the 8th needs to be a letter. I have come up with this so far but i believe it could be a lot cleaner, it works however!! As i am typing this i now know that the 8th char whilst unable to be a digit, can be a symbol,, hmm how to go about this??

    Just wondering if you guys have any thoughts for me

    void checkStudentData(String studentID){
    if (studentID == null){
    while (studentID == null){
    try {
    Scanner sc = new Scanner(System.in);
    System.out.print("Please enter student ID to check" +
    "\n ID must be 7 digits and 1 character\n");
    String sID = sc.next();
    if(String.valueOf(sID).trim().length() != 8)
    throw new Exception();
    else if (Character.isDigit(sID.charAt(7)))
    throw new Exception();
    else{
    int i;
    for ( i= 0; i<7; i++){
    if(Character.isDigit(sID.charAt(i)))
    studentID = sID;
    else
    throw new Exception();
    }
    }
    }

    catch(Exception e){
    System.out.println("Invalid Student ID");
    }
    }
    }
    }

    Thanks

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    You say that you want to verify the code from client, once the client enter the code you want to check that it's in correct format. Is it?

    Such validations we can do easily using regular expressions. Or else in simple logic, like the code length should be 8, and the last digit should be a letter. Validation of the last digit can be done in this way, convert the digit to a number, if it's a letter or a symbol you comes with an exceptions, means it's not a number. Those trick things we can do sometimes, I'm doing it.

Similar Threads

  1. Convert java code to midlet code
    By coldvoice05 in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 11:14 AM
  2. Convert java code to midlet code
    By coldvoice05 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 01:21 PM
  3. Why doesn't this code accept my code?
    By PeterFeng in forum New To Java
    Replies: 5
    Last Post: 02-03-2009, 01:39 PM
  4. I need help fixing my code.. or non code?
    By MrHuggykins in forum New To Java
    Replies: 1
    Last Post: 03-19-2008, 10:12 PM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •