简体   繁体   中英

Sum of array elements in java

I want to do the sum of array elements. I made this but I can't see what is my mistake? Thanks in advance.

Input Format

The first line contains a single integer N denoting the size of the array. (eg 5) The next line contains space-separated integers denoting the elements of the array. (eg 1000000001 1000000002 1000000003 1000000004 1000000005)

Tried following Different ways

import java.io.BufferedReader;
import java.io.InputStreamReader;

//import for Scanner and other utility classes
import java.util.*;


public class TestClass {
    public static void main(String args[] ) throws Exception {
        int sumofArray = 0;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter Size of Array :- ");
        int totalNumbers = Integer.parseInt(br.readLine());
        System.out.println("totalNumbers=> " + totalNumbers);
        int []arrayEle = new int[totalNumbers];
        System.out.println("Enter Array element :- ");
        String arrEle = br.readLine();
        System.out.println("Array Elements Are:-"+arrEle);
        String []myArray = arrEle.split(" ",totalNumbers+1);
        for(int i = 0; i < totalNumbers; i++) {
            arrayEle[i] = Integer.parseInt(myArray[i]);
            System.out.println(arrayEle[i]);
            //sumofArray = sumofArray + arrayEle[i];
            //System.out.println("i= "+i+" arrayEle[i]= "+arrayEle[i] + " sumofArray= "+sumofArray);
        }
        for(int i = 0; i < totalNumbers; i++) 
            sumofArray += arrayEle[i];
        System.out.println("Sum of Array Elements is:-  "+sumofArray);
        //Scanner s = new Scanner(System.in);
        //String name = s.nextLine();

    }
}

But unable to understand, why i'm ending with this result

Enter Size of Array :- 
totalNumbers=> 5
Enter Array element :- 
Array Elements Are:-1000000001 1000000002 1000000003 1000000004 1000000005
1000000001
1000000002
1000000003
1000000004
1000000005
Sum of Array Elements is:-  705032719

Your sumofArray variable is int. When you add those 5 numbers, integer variable can't store the value. The upper limit of int value in java is 2147483647. Therefore, you are getting an unexpected answer. If you change the data type of sumofArray to long, it will work.

In java, int, long and double is primitive data types that have some max length in memory.

int: -2147483648 to 2147483647

you can think of it as a loop, when your limit increases it doesn't throw an error. If you will add 2147483647+1 to the max number it will give you -2147483648

int a = 2147483647;
a+1; // -21474836486

It's true for every primitive data type so you have to always look for the max values of every data types.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM