# Having trouble indentifing an error

Page 1 of 1

## 4 Replies - 522 Views - Last Post: 01 March 2013 - 10:52 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=313964&amp;s=02bd1f060f736a7076bfe842300fa1bc&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 SpartanGiani

Reputation: 0
• 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
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

• D.I.C Lover

Reputation: 3534
• Posts: 16,016
• Joined: 20-September 08

## Re: Having trouble indentifing an error

Posted 01 March 2013 - 09:44 AM

### #3 Kinaces

Reputation: 78
• 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

### #4 SpartanGiani

Reputation: 0
• 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

### #5 pbl

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

Reputation: 8378
• Posts: 31,956
• 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