4 Replies - 186 Views - Last Post: 01 March 2013 - 10:52 AM Rate Topic: -----

#1 SpartanGiani  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 26-February 13

Having trouble indentifing an error

Posted 01 March 2013 - 09:40 AM

Code:


import java.util.Scanner;
public class Lab7
{
public static void main (String[] args)
{

int x = 0;
int y = 0;
Scanner input = new Scanner(System.in);
System.out.print("how many points: ");
int n = input.nextInt();
double [][] points = new double [n][2];
double [][] distances = new double [n][n];
double [] averages= new double [n];
for (int i= 0; i < n; i++)
{
System.out.print(i + "Enter your X: ");
points[i][0] = input.nextDouble();
System.out.print(i + "Enter your Y: ");
points[i][1] = input.nextDouble();
}

{
for (int i= 0; i < n; i++)
for (int j= 0; j < n; j++)
{
distances [i][j] = getdistances ( points, x, y ); y++;
}
x++;
y = 0;
}

x = 0 ; y = 1;

double min = 0;

for (int i= 0; i < n; i++)
averages [i] = getaverages(distances, x, y);

for (int i = 0; i < n; i++)

if (min > averages[i])
{
min = averages[i];
}

}
public static double getdistances(double[][] points, int x, int y)
{
int n = points.length;
double result = 0;
if (y != n && x != n)
result = Math.abs(points[x][0] - points[y][1]);

return result;

}


public static double getaverages(double[][] distances, int x, int y)
{
double result = 0;
for (int i= x; i < y; i++)
{
for (int j= 0; j < distances.length; j++)
{
result = result + distances [i][j];
}
}

return result;
}
}




Output with error:


----jGRASP exec: java Lab7

how many points: 3
0Enter your X: 1
0Enter your Y: 5
1Enter your X: 4
1Enter your Y: 6
2Enter your X: 1
2Enter your Y: 3
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at Lab7.getdistances(Lab7.java:53)
at Lab7.main(Lab7.java:27)

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

Is This A Good Question/Topic? 0
  • +

Replies To: Having trouble indentifing an error

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2728
  • View blog
  • Posts: 11,470
  • Joined: 20-September 08

Re: Having trouble indentifing an error

Posted 01 March 2013 - 09:44 AM

Please Posted Image
Was This Post Helpful? 0
  • +
  • -

#3 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: Having trouble indentifing an error

Posted 01 March 2013 - 09:45 AM

I'm not going to take a look at the code since it's not in code tags, and is hard to look at otherwise.

You are receiving an ArrayIndexOutOfBoundsException which means you are trying to access the array at an index that doesn't exist (Meaning you have a negative number or the number is bigger than the array size). Make sure you are accounting for arrays starting at 0 not 1.

This post has been edited by Kinaces: 01 March 2013 - 09:47 AM

Was This Post Helpful? 1
  • +
  • -

#4 SpartanGiani  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 26-February 13

Re: Having trouble indentifing an error

Posted 01 March 2013 - 09:47 AM

Ok thanks,I understand now what the problem is, thanks I appreciate it
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Having trouble indentifing an error

Posted 01 March 2013 - 10:52 AM

Learn out to read stack trace

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4[b]
at Lab7.[b]getdistances(Lab7.java:53)


you are doing array[4] which exceed the array size
at line 53 in Lab7.java in method getdistances
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1