This is for my AP Computer Programming class and I am lost at whats wrong with my code. My other programming teacher basically sees nothing wrong with my code and I've tried various different sets of code to work, but none have. This code, however seems the most likely to work.
int[] d = {8, 7, 6, 2 };
boolean valid;
int sum = 0;
int dd;
for ( int i = 0; i < d.length; i++ )
{
if ((d[d.length - i] %10) == 0 )
{
dd = d[d.length - i] * d[d.length - 1];
sum += dd ;
}
else
{
sum += d[d.length - i] ;
}
}
if ( sum %10 == 0)
{
valid = true;
}
else
{
valid = false;
}
What am I doing wrong. Here's the error that is coming up
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at TC1.work(TC1.java:24)
at TC1.main(TC1.java:12)