I have the code to check a text file line-by-line but I am unsure how to check for a specific format. If the text file is in the following format print out the content, otherwise dipslay a message saying "File is not correct."
The first line of the should start with an S, and every line consists of any upper-case, followed by a colon, than any letter, number or e.
You can use regular expressions to easily validate the format:
//first line validator:
String reg1 = "S:[A-Z01e]";
//next lines validator:
String reg2 = "V:[A-Z01e]?[A-Z01e]?[A-Z01e]";
//examples/test cases:
System.out.println("S:1".matches(reg1));
System.out.println("S:3".matches(reg1));
System.out.println("S:11".matches(reg1));
System.out.println("V:1e0".matches(reg2));
System.out.println("V:1e01".matches(reg2));
System.out.println("V:1e3".matches(reg2));
The regex is: First S or V respectively, then :, and then any character between A-Z ot 0 or 1 or e.
The ? means 0 or one repeats, so [A-Z01e]?[A-Z01e]?[A-Z01e] means 1-3 characters, each A-Z or 0 or 1 or e.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With