Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Java (https://www.askmehelpdesk.com/forumdisplay.php?f=440)
-   -   Converting chars to digits and digits to one String (https://www.askmehelpdesk.com/showthread.php?t=811348)

  • May 4, 2015, 05:11 AM
    inout
    Converting chars to digits and digits to one String
    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
  • Jun 23, 2015, 02:50 AM
    George985
    1 Attachment(s)
    Propably a little late answer, but, the thing you need is the function String.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.

    Code:

    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.

  • All times are GMT -7. The time now is 03:11 AM.