'char array to int array

I'm trying to convert a string to an array of integers so I could then perform math operations on them. I'm having trouble with the following bit of code:

String raw = "1233983543587325318";

char[] list = new char[raw.length()];
list = raw.toCharArray();
int[] num = new int[raw.length()];

for (int i = 0; i < raw.length(); i++){
    num[i] = (int[])list[i];
}

System.out.println(num);

This is giving me an "inconvertible types" error, required: int[] found: char I have also tried some other ways like Character.getNumericValue and just assigning it directly, without any modification. In those situations, it always outputs the same garbage "[I@41ed8741", no matter what method of conversion I use or (!) what the value of the string actually is. Does it have something to do with unicode conversion?



Solution 1:[1]

Two ways in Java 8:

String raw = "1233983543587325318";

final int[] ints1 = raw.chars()
    .map(x -> x - '0')
    .toArray();

System.out.println(Arrays.toString(ints1));

final int[] ints2 = Stream.of(raw.split(""))
    .mapToInt(Integer::parseInt)
    .toArray();

System.out.println(Arrays.toString(ints2));

The second solution is probably quite inefficient as it uses a regular expression and creates string instances for every digit.

Solution 2:[2]

Everyone have correctly identified the invalid cast in your code. You do not need that cast at all: Java will convert char to int implicitly:

String raw = "1233983543587325318";

char[] list = raw.toCharArray();
int[] num = new int[raw.length()];

for (int i = 0; i < raw.length(); i++) {
    num[i] = Character.digit(list[i], 10);
}

System.out.println(Arrays.toString(num));

Solution 3:[3]

You shouldn't be casting each element to an integer array int[] but to an integer int:

for (int i = 0; i > raw.length(); i++)
{
   num[i] = (int)list[i];
}

System.out.println(num);

Solution 4:[4]

this line:

num[i] = (int[])list[i];

should be:

num[i] = (int)list[i];

Solution 5:[5]

You can't cast list[i] to int[], but to int. Each index of the array is just an int, not an array of ints.

So it should be just

num[i] = (int)list[i];

Solution 6:[6]

For future references. char to int conversion is not implicitly, even with cast. You have to do something like that:

    String raw = "1233983543587325318";

    char[] list = raw.toCharArray();
    int[] num = new int[list.length];

    for (int i = 0; i < list.length; i++){
        num[i] = list[i] - '0';
    }
    System.out.println(Arrays.toString(num));

Solution 7:[7]

This class here: http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html should hep you out. It can parse the integers from a string. It would be a bit easier than using arrays.

Solution 8:[8]

Everyone is right about the conversion problem. It looks like you actually tried a correct version but the output was garbeled. This is because system.out.println(num) doesn't do what you want it to in this case:) Use system.out.println(java.util.Arrays.toString(num)) instead, and see this thread for more details.

Solution 9:[9]

String raw = "1233983543587325318";
char[] c = raw.toCharArray();
int[] a = new int[raw.length()];

for (int i = 0; i < raw.length(); i++) {
   a[i] = (int)c[i] - 48;
} 

Solution 10:[10]

You can try like this,

String raw = "1233983543587325318";

char[] list = new char[raw.length()];
list = raw.toCharArray();
int[] num = new int[raw.length()];

for (int i = 0; i < raw.length(); i++) {
    num[i] = Integer.parseInt(String.valueOf(list[i]));
}

for (int i: num) {
    System.out.print(i);
}

Solution 11:[11]

Simple and modern solution

int[] result = new int[raw.length()]; 
Arrays.setAll(result, i -> Character.getNumericValue(raw.charAt(i))); 

Solution 12:[12]

Line num[i] = (int[])list[i];

It should be num[i] = (int) list[i]; You are looping through the array so you are casting each individual item in the array.

The reason you got "garbage" is you were printing the int values in the num[] array. char values are not a direct match for int values. char values in java use UTF-16 Unicode. For example the "3" char translates to 51 int

To print out the final int[] back to char use this loop

for(int i:num)
        System.out.print((char) i);