1

I'm using Android Studio and I have this String in one of my activities:

[0, 25, 24, 25, 15, 16, 17, 25, 24, 21, 24]

and I want to convert it to a int[] :

{0, 25, 24, 25, 15, 16, 17, 25, 24, 21, 24};

How do I do that? I tried the code below, but it's not returning the correct value.

int[] intArray = new int[string.length()];

for (int i = 0; i < string.length(); i++) {
    intArray[i] = Character.digit(string.charAt(i), 10);
}
0

3 Answers 3

2

This is called "parsing". You will need to solve this with your own code. There is no built-in method in Java or Android to do so. You should look at the methods available in the String class, especially matches() and split().

Sign up to request clarification or add additional context in comments.

Comments

1

On Android, you can make use of org.json.JSONArray:

JSONArray jsonArray = new JSONArray(arr); // throws JSONException
int length = jsonArray.length();
int[] results = new int[length];

for (int i = 0; i < length; ++i) {
  results[i] = jsonArray.get(i);
}

Comments

1
String[] arr = [...];
int[] ints = new int[arr.length()];
for(int i = 0 ; i < arr.length() ; i++){
    ints[i] = Integer.parseInt(arr[i]);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.