-
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
-
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.