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.
Having trouble indentifing an error
Page 1 of 14 Replies - 69 Views - Last Post: 01 March 2013 - 10:52 AM
Replies To: Having trouble indentifing an error
#3
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.
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
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
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
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
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|