Answer the question
In order to leave comments, you need to log in
Parse String to int array - Java, how to implement?
There is a text field of type string:
[3389, 3410, 3430, 3450, 3470, 3500, 3520, 3540, 3760, 3780, 3810, 3830, 3850, 3970, 4000, 4140, 4160, 4180, 4210, 4220, 4250, 4300, 4320, 4340, 4360, 4380, 4400, 4420, 4430, 4440]
Answer the question
In order to leave comments, you need to log in
Help with this option.
private String b = "[3389, 3410, 3430, 3450, 3470, 3500, 3520, 3540, 3760, 3780, 3810, 3830, 3850, 3970, 4000, 4140, 4160, 4180, 4210, 4220, 4250, 4300, 4320, 4340, 4360, 4380, 4400, 4420, 4430, 4440]";
String[] items = b.replaceAll("\\[", "").replaceAll("\\]", "").split(",");
int[] results = new int[items.length];
for (int i = 0; i < items.length; i++) {
try {
results[i] = Integer.parseInt(items[i].trim());
} catch (NumberFormatException nfe) {};
}
Log.d("TEST", Arrays.toString(results));
First, we split the string .split(",\s");
into an array of strings. We create an int array of the same length. Then we loop through the arrays and convert the strings to numbers element by element. numbres[i] = Integer.parseInt(strings[i]);
UPD: This is NOT actually the best solution. I somehow immediately overlooked that this string is a valid object in JSON format, and should be parsed by the appropriate tools. Correct answers given by: EugeneP2 and OnYourLips
This is json.
www.oracle.com/technetwork/articles/java/json-1973...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question