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?

6

13 Answers

There are a number of issues with your solution. The first is the loop condition i > raw.length() is wrong - your loops is never executed - thecondition should be i < raw.length()

The second is the cast. You're attempting to cast to an integer array. In fact since the result is a char you don't have to cast to an int - a conversion will be done automatically. But the converted number isn't what you think it is. It's not the integer value you expect it to be but is in fact the ASCII value of the char. So you need to subtract the ASCII value of zero to get the integer value you're expecting.

The third is how you're trying to print the resultant integer array. You need to loop through each element of the array and print it out.

    String raw = "1233983543587325318";

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

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

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

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.

1

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));
1

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);

this line:

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

should be:

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

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];

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));
1

This class here: should hep you out. It can parse the integers from a string. It would be a bit easier than using arrays.

2

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.

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;
} 

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);
}

Simple and modern solution

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

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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest