Issue
I need to validate the lines of an input. But each line can only contain 2 numbers separated by 1 comma. Example of format:
1,4
2,7
8,9
Also the first number must be under a certain max (my program checks the max value possible by a id.size() method, it gives the size of a list and that must be the maximum number possible for the first number of the string).
Second number can be any value.
Solution
You can use regular expressions to validate that you have two numbers separated by a comma, if you are only working with integers.
if(str.matches("-?[0-9]+,-?[0-9]+")){
//valid
} else {
//invalid
}
After that, you can use String#split
along with Integer.parseInt
to get the two numbers.
final String[] parts = str.split(",");
final int num1 = Integer.parseInt(parts[0]);
final int num2 = Integer.parseInt(parts[1]);
if(num1 < MAX_VALUE_FOR_FIRST_NUM){
//valid
} else {
//invalid; display error message
}
Answered By - Unmitigated