PDA

View Full Version : Converting chars to digits and digits to one String


inout
May 4, 2015, 05:11 AM
I split a String to char convert every single char to a digit.Then I want to put all digits from the string given together in an array.Any idea how to do it?
What I have done is I have a switch for converting chars to digits the output of every case gives me a digit but I want to have for the input String the output in digits .T

George985
Jun 23, 2015, 02:50 AM
Propably a little late answer, but, the thing you need is the function String.toCharArray (http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#toCharArray())().
So what you would do is take the string you want, use the toCharArray function, then convert each char to digit and put in in the array.



String my_string = "random string";
int[] digit_array = new int[my_string.toCharArray().length];
int index = 0;
for( char c : my_string.toCharArray() ) {
digit_array[index] = char_to_digit_function(c);
index++;
}


A full working example is attached as .txt file.