0
  Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
  at newpackage.NewClass5.main(NewClass5.java:33)
  at if(average[k]>second) and i dont know why its giving me that

My code gives me that error i want to find and remove the minimum and maximum of each line then calculate the average of each one and make the find the the winner

Shaun 8.4 5.6 7.2 9.2 8.1 5.7

George 5.4 8.7 6 9.9 7.6 8.3

Kim 7.5 5.2 5.9 9.1 8.5 9

  Scanner input=new Scanner(System.in);
  System.out.println("Enter the number of Swimmers");
  int n = input.nextInt();
  String[] name = new String[n];
  System.out.println("Enter the number of Juries");
  int m = input.nextInt();
  double[] jurie=new double[m];
  double[] average=new double[n];
  for(int i=0;i<n;i++){
  name[i] = input.next();
  for(int pidh=0;pidh<m;pidh++){
  jurie[i]=input.nextDouble();
  double total = 0.0;
  double max1 = jurie[pidh];
  double min = jurie[pidh];
  for(int q=0;q<m;q++){
  if(jurie[pidh] > max1){
                max1 = jurie[pidh];}
        if(jurie[pidh] < min){
           min = jurie[pidh];}
        total=total + jurie[i];
       }
        average[i]=(total-(min+max1))/(m-2);
        }

    int pos=0;
    double second = average[0];
       for(int k=0;k<m;k++){
                 if(average[k]>second)
        {
            second = average[k];
            pos = k;
        }
    }
    System.out.println("\n" +name[pos] + " is the winner with " + second +  " points.");  
  }
   }
   }
Tom
  • 16,842
  • 17
  • 45
  • 54
Karoqi
  • 3
  • 4

2 Answers2

0

K is dependent on m and average is size n. If m is bigger than n you would get the exception. Try to clean up your code. It is hard to read.

Also place a breakpoint in the offending line. Then inspect the values.

bichito
  • 1,406
  • 2
  • 19
  • 23
0

You should change:

for(int k=0;k<m;k++){

to:

for(int k=0;k<n;k++){

as n holds the number of swimmers for which you're calculating the averages (per the code double[] average=new double[n];).

AntonH
  • 6,359
  • 2
  • 30
  • 40
  • Now it worked! thanks, if anyone still looks this thread. the values it prints are more than 10 but i want to average the correct numbers , it gives me 8.55 , 12.45 , 13.5 – Karoqi Feb 28 '17 at 19:00
  • @Karoqi You're welcome :) – AntonH Feb 28 '17 at 20:56