1

My objective is to "Read into array A 10 numbers from the keyboard. Determine the average of the elements of array A. Store in array B the difference of each element of array A and the average of all the elements in array A". However, as I tried to code this, I ended up dealing with an error whenever I enter the last element for Array A. Here is my source code:

import java.util.*;
public class PROJECT_8 {
 public static void main(String [] args) {
Scanner m = new Scanner(System.in);

double A[] = new double[10];
double B[] = new double[10];
int i, ctr = 1;
double sum = 0;

System.out.println("Enter 10 numbers: ");
for(i=0; i<A.length; i++)
{
  A[i] = m.nextDouble();
  sum+=A[i];
  A[i] = sum/ctr;
  ctr++;
}

for(i=1; i<A.length; i++)
{
  if(A[i] != A[0])
  {
    A[i] = A[0] - A[i];
    A[i] = B[i];
    A[0]+=1;
  }
}

System.out.println("Array A = " + A[i]);
System.out.println("Array B");
for(i=0; i<length; i++)
    System.out.println(B[i]);

  }
}

Here is the error I received:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 10

I don't really understand the issue as I have already applied the "i=0" concept; even when I changed the i=1 by the second for loop, the issue still remained.

What alterations do I have to perform on the latter?

Thank you so much for your assistance and have a nice day.

Novice
  • 11
  • 3
  • 1
    The code you show doesn't fulfill the requirements of a [mcve] as it shouldn't even build. Please [edit] your question to improve it. – Some programmer dude Nov 30 '20 at 04:59
  • On another note, what purpose do the array `B` serve? You never assign to its elements. – Some programmer dude Nov 30 '20 at 05:00
  • I tried to store the differences between every 2 elements for Array A to Array B. How can I do it properly? – Novice Nov 30 '20 at 05:07
  • First regarding the build errors, what is `length`? In the last loop you use the undefined variable `length`. Secondly about `B`, assignment goes from right to left, i.e. it's the left-hand side of the assignment that is the destination, and the right-hand side that is the source. Now think about the assignment `A[i] = B[i]` again. – Some programmer dude Nov 30 '20 at 05:08
  • For the last for loop, "length" was supposed to be "A.length"; I apologize for the typo. I also switched the positions of B[i] and A[i]; however, the results stayed the same. – Novice Nov 30 '20 at 05:13
  • You have the loops `for(i=1; i – Some programmer dude Nov 30 '20 at 05:16
  • System.out.println("Array A = " + A[i]); This instruction causes the error, because the for loop just before has incremented i until 10 . – nissim abehcera Nov 30 '20 at 05:16
  • @Novice I think you are on the right track, you might just want to break up your logic into a couple different methods, here's an attempt to do that: https://repl.it/@ShashSinha/SmugBossyDirectory#Main.java – Sash Sinha Nov 30 '20 at 05:18
  • @ShashSinha I'm somewhat confused with the sample you sent me, forgive me as I've only started learning Java a few months ago, but I'll try my best to understand it. Thank you very much! – Novice Nov 30 '20 at 05:25
  • @Someprogrammerdude duly noted, I'm still confused, but I'll try to do so. Thank you! – Novice Nov 30 '20 at 05:26

0 Answers0