-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.

2
  • please add the code you have tried till now. Commented Feb 12, 2016 at 8:50
  • Does this answer your question? Converting a String Array to an Int array Commented Aug 8, 2020 at 17:17

6 Answers 6

8

If you are using java 8 Try this :

 int[] array = Arrays.stream(resultsStr).mapToInt(Integer::parseInt).toArray();
Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

yes, i added the radix in the edit and added it to the wrong place. my bad.
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);
}

}

2 Comments

Integer.valueOf returns an Integer which gets unboxed into an int - you should use Integer.parseInt if you want an int directly.
Thanks for the notice
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.

Comments

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

Comments

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.

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.