I am probably a little late on this, but here is a simple solution that basically uses a for loop to iterate through the array of strings, adds the parsed integer value to a new integer array, and adds the integers up as it goes. Also note that I moved the square brackets for the String array type, String[] results
, because not only is it less confusing but the array is part of the type and not a part of the arrays name.
public class test {
public static void main(String[] args) {
String[] results = { "2", "1", "5", "1" };
// Create int array with a pre-defined length based upon your example
int[] integerArray = new int[results.length];
// Variable that holds the sum of your integers
int sumOfIntegers = 0;
// Iterate through results array and convert to integer
for (int i = 0; i < results.length; i++) {
integerArray[i] = Integer.parseInt(results[i]);
// Add to the final sum
sumOfIntegers += integerArray[i];
}
System.out.println("The sum of the integers is: " + sumOfIntegers);
}
}
You could also create the integer array first, and then after the loop add the numbers together, but in this instance we are assuming that the array of Strings are all integers so I didn't see a reason to make this too complicated.