is there a way in Java to convert an Integer to single digits, and vice versa. Like this:
So I have the number 345. I want to break it down to 3, 4 and 5 – three seperate numbers.
I have the numbers 3, 4 and 5. I want to put them together to make 345?
Well you can easily convert it to a character array, and go from there:
(You don’t really need the char array here – you could just use the string and use
length()andcharAt()instead oflengthand the indexer – but I find this clearer.)Then to reassemble, just do the reverse – create a
char[]from the digits (by adding'0'to each), then create a string from thechar[], then useInteger.parseInt.