As much as I usually abhor regular expressions this is a great place for using them. You get data validation as well as parsing all in one shot with the correct use of grouping.
^(\d{4})?(-)?(\d{4})?$ will give you 3 output groups, you check each one to see if you got a value and make decisions based on that, and as a bonus you are guaranteed that they are 4 digit numbers. Look at the groups on the Rubular page I link to. If you have a value in group 1 then that is the start year, a "-" in group two then it is a range, a value in group 3 an end range. Makes the logic really simple and you don't have to do all that silly string manipulation and Integer testing that other solutions suggest.
Since this is homework, here is something to kick start your thinking, how to use the Pattern
and Matcher
, I am not going to do the entire thing for you.
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main
{
public static void main(String[] args)
{
final List<String> inputs = Arrays.asList("1999-", "-2010", "1990-2000", "1967");
final Pattern p = Pattern.compile("^(\\d{4})?(-?)(\\d{4})?$");
int count = 0;
for (final String input : inputs)
{
final Matcher m1 = p.matcher(input);
m1.find();
System.out.format("Input: %d\n", count++);
for (int i=1; i <= m1.groupCount(); i++)
{
System.out.format("\tGroup %d = %s\n", i, m1.group(i));
}
}
}
}
Here is the expected output:
Input: 0
Group 1 = 1999
Group 2 = -
Group 3 = null
Input: 1
Group 1 = null
Group 2 = -
Group 3 = 2010
Input: 2
Group 1 = 1990
Group 2 = -
Group 3 = 2000
Input: 3
Group 1 = 1967
Group 2 =
Group 3 = null
How it works:
^
- start at the beginning of the line
(\d{4})?
- this looks for exactly 4 digits \d
and the ()
says group what you find, ?
means this group is optional for a match, group will == null
(-)?
- this looks for the dash, ?
makes it optional, ()
groups the results at above
(\d{4})?
- this looks for exactly 4 digits \d
and the ()
says group what you find, ?
means this group is optional for a match, group will == null
$
- matches the end of the line
Exercise left for the reader: The two cases it doesn't "validate" for is an empty string and "-", both of which should be considered "special" cases depending on what you want your business logic to be.