Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Having trouble indentifing an error

$
0
0
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.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>