tags:

views:

542

answers:

2

this article suggests you can use Color c = Color.decode("FF0096"); however this understandably throws an exception

Caused by: java.lang.NumberFormatException: For input string: "FF0096"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:449)
    at java.lang.Integer.valueOf(Integer.java:528)
    at java.lang.Integer.decode(Integer.java:958)
    at java.awt.Color.decode(Color.java:707)

What is the best way of converting a String in the format of "#FF0096" or "FF0096" into a java awt Color?

+3  A: 
Color c = Color.decode("0xFF0096");

or

Color c = Color.decode("#FF0096");

or

Color c = new Color(0xFF0096);
Matthew Flaschen
+2  A: 

The Color.decode method throws the NumberFormatException if the specified string cannot be interpreted as a decimal, octal, or hexidecimal integer

The string "FF0096" without the prefix of 0 or 0x will be interpreted as a base 10 representation which does not work.

codaddict
you are wrong to say that FF0096 prefixed with 0x does not work.
pstanton
@pstanton: Thanks for pointing out..it was a typo. I've corrected it.
codaddict