Yes, you can do it with a regex.
One way to do this is to check if for each row you read, you have digits only, and add a constraint for the number of digits. The digits part can be done pretty easily, using a “/d
” while the number of characters to be used is constrained by “{desiredNumber}
“.
To better understand regex in Java, use this link 😀
If you still cannot solve it, this is the magic line:
if (!newLine.matches("\\d{6}")) {
return false;
}
6
solved Checking data string fits a 6 digit number format from input file [closed]