I'm taking user input from a file in form (1, 2, 3)
to create a color, and I just wanted to know if I was taking most cases into account, and if there is any way I could improve this method in terms of being robust and readable.
private Color getColorFromString(String rgb) throws MalformedColorException{
rgb = rgb.replaceAll("[()\\s]", "");
String[] colors = rgb.split(",");
if(colors.length > 3){
throw new MalformedColorException("Too many arguments");
}else if (colors.length < 3){
throw new MalformedColorException("Too little arguments");
}
try{
int red = Integer.parseInt(colors[0]);
int green = Integer.parseInt(colors[1]);
int blue = Integer.parseInt(colors[2]);
return new Color(red, green, blue);
}catch(NumberFormatException e){
throw new MalformedColorException("Malformed number", e);
}
}