Documentation is not for reading

Or so it seems. Lately a bunch of bugs have haunted me that were caused by not having read the documentation thoroughly.

This time I wanted to convert the typical hex rgb color strings to integers to be used by the java.awt.Color class (I didn’t program the lib that uses this class!).

My first try:

string = #ff9933;
int r = Integer.decode(string.substring(0,2)).intValue() ;
int g = Integer.decode(string.substring(2,4)).intValue();
int b = Integer.decode(string.substring(4)).intValue();
this.color = new Color(r,g,b) ;

It might have helped to read the documentation

Decodes a String into an Integer. Accepts decimal, hexadecimal, and octal numbers numbers given by the following grammar:

from the Java 1.4.1 API: Integer

I might have payed attention to one of my favorite words which was even in that sentence: grammar! I would have noticed that before each hex number a “#” is required, so that it can be recognized. Ouch. My second try is a bit shorter and seems to work just fine (that is until I can test wether the color is the one I want:

this.color = new Color(Integer.decode(string).intValue());

By Yashima

Writer of code and stories