-1

I have an string array in java program like this

String[] results = { "2", "1", "5", "1" };

I want to convert this to integer array as like this:

int[] results = { 2, 1, 5, 1 };

And finally I want to find the summation of all the int elements of that array.

flavio.donze
  • 7,432
  • 9
  • 58
  • 91
Ruhul Amin
  • 11
  • 1
  • 1
  • 1
  • please add the code you have tried till now. – phoenix Feb 12 '16 at 08:50
  • Does this answer your question? [Converting a String Array to an Int array](https://stackoverflow.com/questions/21677530/converting-a-string-array-to-an-int-array) – Mr. Jain Aug 08 '20 at 17:17

6 Answers6

8

If you are using java 8 Try this :

 int[] array = Arrays.stream(resultsStr).mapToInt(Integer::parseInt).toArray();
soorapadman
  • 4,451
  • 7
  • 35
  • 47
1
String resultsStr[] = {"2", "1", "5", "1"};
ArrayList intermediate = new ArrayList();
for(String str : resultsStr)
{
    intermediate.add(Integer.parseInt(str, 10)); //the 10 could be ommitted
}
int resultsInt[] = intermediate.toArray();

and your resultsInt[] will contain the array of ints. While I agree that it doesn't have to go thru the arraylist (it can be accomplished without it) i used it merely because it was easier to type out.

Shark
  • 6,513
  • 3
  • 28
  • 50
0

This is my simple solution :

public class Tester {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    String results[]={"2","1","5","1"};

    //Creating a new array of Type Int
    int result [] = new int[4];

    int sum = 0;

    //Going trough every element in results And Converting it to Int By using : Integer.valueOf(String str)

    for (int i = 0; i < results.length; i++) {
        result[i] = Integer.valueOf(results[i]);

    }


    //Displaying the result
    for (int i = 0; i < result.length; i++) {
        System.out.println(result[i]);
        sum += result[i];
    }

    System.out.println("The sum of all elements is : " + sum);
}

}

0

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.

Ron
  • 151
  • 2
0

A simple solution will be:

 private void toIntArray(String[] strings) {
    Integer[] intArray=new Integer[strings.length];
    int i=0;
    for(String str:strings){
        intarray[i]=Integer.parseInt(str.trim());
        i++;
    }
 }
sudesh regmi
  • 536
  • 4
  • 12
0

This code converts the String array to an int array and calculate the sum of all int array elements:

String[] input = { "2", "1", "5", "1" };
int[] results = Stream.of(input).mapToInt(Integer::parseInt).toArray();
int sum = Arrays.stream(results).sum();
System.out.println("SUM: " + sum);

The shorter version if you don't require the int[] array:

String[] input = { "2", "1", "5", "1" };
int sum = Stream.of(input).mapToInt(Integer::parseInt).sum();
System.out.println("SUM: " + sum);

This works for Java 8 and higher.

flavio.donze
  • 7,432
  • 9
  • 58
  • 91