# Is my code working

• October 29th, 2013, 07:00 PM
return0
Is my code working
Hello all, I am on a computer that I cannot download anything on and it does not have jdk on it so I cannot test my code to see if it is running. Anyone mind running it for me to see if all is right? I believe it is, but I could be wrong. What my code does is add two integers of up to 50 digits each together.
Code :

```import java.util.* ;   public class Calculator { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print("Enter the first integer:"); String a = s.nextLine(); System.out.print("Enter the second integer:"); String b = s.nextLine(); int[] num1 = new int[a.length()]; int[] num2 = new int[b.length()]; for (int i=0;i<a.length();i++) { num1[i] = (int) a.charAt(i); num2[i] = (int) b.charAt(i); } int[] sum = new int[a.length()]; for (int n=b.length()-1;n>=0;n--) { if (num1[n]+num2[n]<10) { sum[n]=num1[n]+num2[n];   } if (num1[n]+num2[n]>=10) { sum[n]=(num1[n]+num2[n])%10; sum[n-1]=sum[n-1]+1; } } for (int d=0; d<a.length();d++) { System.out.print(sum[d]+" "); } } }```
Thanks for any help!
• October 29th, 2013, 07:11 PM
GoodbyeWorld
Re: Is my code working

```
Enter the first integer: 9
Enter the second integer: 2
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at Calculator.main(Calculator.java:27)
```

It points to this line: sum[n-1]=sum[n-1]+1;
• October 29th, 2013, 07:42 PM
return0
Re: Is my code working
Thanks for doing that I really appreciate it. I'm not sure what is wrong with that line of code I wrote though. Anybody know what I'm doing wrong here?
Code :

`sum[n-1]=sum[n-1]+1;`

--- Update ---

I looked up the error online but still am not grasping the meaning of it.
• October 29th, 2013, 09:14 PM
Norm
Re: Is my code working
Quote:

ArrayIndexOutOfBoundsException
Here's code that will get that error:
Code :

```int[] intA = new int[2]; // has 2 elements: valid indexes are: 0 and 1 intA[3] = 4; // ERROR: 3 is out of bounds intA[-1] = 55; // ERROR: -1 is out of bounds```
• October 29th, 2013, 09:39 PM
return0
Re: Is my code working
So would that code example you showed me compile like my program did then show an error? How come it lets my program compile and lets the user enter the integers before showing the error?
• October 29th, 2013, 09:48 PM
Norm
Re: Is my code working
Yes, it would compile without any errors. Maybe a really good compiler could look at the code and give an error or warning, but none that I know of. Normally code uses a variable as an index instead of hardcoding values like above:
Code :

``` int[] intA = new int[2]; // has 2 elements: valid indexes are: 0 and 1 // ... int ix = 3; // will be index that is out of bounds // then many statements later   intA[ix] = 4; // ERROR: 3 is out of bounds```

Quote:

How come it lets my program compile and lets the user enter the integers before showing the error?
That's the way programs work. It is up to the programmer to write code to detect when an index is out of bounds and not use it.