Results 1 to 2 of 2
Thread: Code streamlining
- 08-23-2009, 09:05 AM #1
Member
- Join Date
- Aug 2009
- Posts
- 1
- Rep Power
- 0
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
- 08-23-2009, 03:32 PM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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
-
Convert java code to midlet code
By coldvoice05 in forum New To JavaReplies: 1Last Post: 08-12-2009, 11:14 AM -
Convert java code to midlet code
By coldvoice05 in forum Advanced JavaReplies: 1Last Post: 08-09-2009, 01:21 PM -
Why doesn't this code accept my code?
By PeterFeng in forum New To JavaReplies: 5Last Post: 02-03-2009, 01:39 PM -
I need help fixing my code.. or non code?
By MrHuggykins in forum New To JavaReplies: 1Last Post: 03-19-2008, 10:12 PM -
Generating Code Automatically Using Custom code Template In Eclipse
By JavaForums in forum EclipseReplies: 1Last Post: 04-26-2007, 03:52 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks