I have to parse a String that can assume hex values or other non-hex values
0xff
, 0x31
or A
, PC
, label
, and so on.
I use this code to divide the two cases:
String input = readInput();
try {
int hex = Integer.decode(input);
// use hex ...
} catch (NumberFormatException e) {
// input is not a hex, continue parsing
}
Can this code be considered "ugly" or difficult to read? Are there other (maybe more elegant) solutions?
EDIT : I want to clarify that (in my case) a wrong input doesn't exist: i just need to distinguish if it is a hex number, or not. And just for completeness, i'm making a simple assebler for DCPU-16.