0
public static void main(String[] args) {
    int n = 5;
     ArrayList<Integer> distances = new ArrayList<Integer> ();
    distances.add(70);
    distances.add(113);
    distances.add(1);
    distances.add(1700000000);
    distances.add(268000);
    int i =0;       
    while(n-->0 & i<5)
    {

        int distance = distances.get(i);

        int au = distance * (149597871 * 1000);
        System.out.println(au);
        i++;
    }
}

So my code outputs this:

720702352
-431854056
-725984360
444733440
-1789971200

But the output should be this:

10471850970000
16904559423000
149597871000
254316380700000000000 
40092229428000000

So my question is how do I fix this issue because I do not understand why it is giving me the wrong product. FYI, if you plug the equation into a calculator it works.

Ajaz Syed
  • 11
  • 2
  • You're overflowing your integers. They only go up to 2 billion or so. – D M Oct 24 '17 at 02:58
  • FYI you should almost always use `&&` for boolean operations. – shmosel Oct 24 '17 at 02:58
  • ok thanks. I'll look at that problem. Sorry. Also thanks for that reminder about the boolean operations. – Ajaz Syed Oct 24 '17 at 03:00
  • Integers can only have a max value of **2147483647** and a minimum value of **-2147483648**. You could I suppose try a Long data type but then again, it can only support a max value of **9223372036854775807** and a minimum value of **-9223372036854775808**. Even this may not be big enough to support some of your calculations. You need to use the [BigInteger Class](http://www.geeksforgeeks.org/biginteger-class-in-java/). This should support whatever you need to do. – DevilsHnd - 退職した Oct 24 '17 at 03:44

0 Answers0