I'm trying to create a program that checks to see whether the user's serial codes are valid or not. It should comply to a specific format. The format should be two numbers, followed by a dash, four numbers, a dot, then four numbers and two characters (note: characters accepted are only a, b and c).
Example valid format:
31-0001.2341ac
00-9999.0001cb
If the serial code is invalid because it didn't meet the required length of string, the program should fill it with zeros at the beginning and the new code will be printed.
I managed to work with the serial code checker using regular expressions, it is now correctly verifying whether the code is valid or not. However, I find it challenging when it needs to generate a new code for an invalid serial code. It's like I need to hard code for all possible combinations.
I tried following this How to format a Java string with leading zero?, but I'm having a hard time because my String format has dash & dot in the middle of the string.
I'm still fairly new, I am not yet familiar with utils or libraries for such functions. I hope someone can help me fix my code to make it simpler and more efficient.
import java.util.Scanner;
public class SerialCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("How many serial numbers would you like to check: ");
int length = sc.nextInt();
int valid = 0;
String[] sSerials = new String[length];
for (int nCtr = 0; nCtr < length; nCtr++) {
System.out.print("Enter Serial " + (nCtr + 1) + ": ");
sSerials[nCtr] = sc.next();
}
System.out.println();
System.out.println("The following were added: ");
for (int nCtr = 0; nCtr < length; nCtr++) {
System.out.println(sSerials[nCtr]);
}
System.out.println();
System.out.println("Comments\t" + "New Code");
for (int nCtr = 0; nCtr < length; nCtr++) {
boolean isValid = sSerials[nCtr].matches("[0-9]{2}-[0-9]{4}\\.[0-9]{4}[abc0-9]{2}");
boolean isMissing = sSerials[nCtr].matches("[0-9]{2}-[0-9]{4}\\.[0-9]{1}[abc0-9]{2}") ||
sSerials[nCtr].matches("[0-9]{2}-[0-9]{4}\\.[0-9]{2}[abc0-9]{2}") ||
sSerials[nCtr].matches("[0-9]{2}-[0-9]{4}\\.[0-9]{3}[abc0-9]{2}");
boolean isMissing1 = sSerials[nCtr].matches("[0-9]{2}-[0-9]{1}\\.[0-9]{4}[abc0-9]{2}") ||
sSerials[nCtr].matches("[0-9]{2}-[0-9]{2}\\.[0-9]{4}[abc0-9]{2}") ||
sSerials[nCtr].matches("[0-9]{2}-[0-9]{3}\\.[0-9]{4}[abc0-9]{2}");
boolean isMissing2 = sSerials[nCtr].matches("[0-9]{0}-[0-9]{4}\\.[0-9]{4}[abc0-9]{2}") ||
sSerials[nCtr].matches("[0-9]{1}-[0-9]{4}\\.[0-9]{4}[abc0-9]{2}");
if (isValid) {
System.out.println("Valid\t\t" + sSerials[nCtr]);
} else if (isMissing) {
System.out.println("Invalid\t\t" + sSerials[nCtr].substring(0, 8) + "0000".substring(0, 14 - sSerials[nCtr].length()) + sSerials[nCtr].substring(8));
} else if (isMissing1) {
System.out.println("Invalid\t\t" + sSerials[nCtr].substring(0, 3) + "0000".substring(0, 14 - sSerials[nCtr].length()) + sSerials[nCtr].substring(3));
} else if (isMissing2) {
System.out.println("Invalid\t\t" + sSerials[nCtr].substring(0, 0) + "00".substring(0, 14 - sSerials[nCtr].length()) + sSerials[nCtr].substring(0));
} else {
System.out.println("Invalid\t");
}
}
}
}