Skip to content

Instantly share code, notes, and snippets.

@cky
Created February 28, 2012 01:40
Show Gist options
  • Save cky/1928425 to your computer and use it in GitHub Desktop.
Save cky/1928425 to your computer and use it in GitHub Desktop.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StudentId {
private static final Pattern REPR_PATTERN = Pattern.compile("S(\\d{1,4})");
private final int studentId;
public StudentId(String repr) {
Matcher m = REPR_PATTERN.matcher(repr);
if (!m.matches())
throw new IllegalArgumentException("Invalid representation: " + repr);
studentId = Integer.parseInt(m.group(1));
}
@Override
public String toString() {
return "S" + studentId;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment