-3
我想从整数数组 读取X,Y值y的值,我的代码:阅读X,从整数数组
class Point
{
int x, y;
}
/**
*
* @author ADMIN
*/
public class Jarvis {
private static Point[] points;
private static boolean CCW(Point p, Point q, Point r)
{
int val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);
if (val >= 0)
return false;
return true;
}
public static void convexHull(Point[] points)
{
int n = points.length;
/** if less than 3 points return **/
if (n < 3)
return;
int[] next = new int[n];
Arrays.fill(next, -1);
/** find the leftmost point **/
int leftMost = 0;
for (int i = 1; i < n; i++) {
if (points[i].x < points[leftMost].x) {
leftMost = i;
}
}
int p = leftMost, q;
/** iterate till p becomes leftMost **/
do
{
/** wrapping **/
q = (p + 1) % n;
for (int i = 0; i < n; i++) {
if (CCW(points[p], points[i], points[q]))
q = i;
}
next[p] = q;
p = q;
} while (p != leftMost);
/** Display result **/
display(points, next);
}
public static void display(Point[] points, int[] next)
{
System.out.println("\nJarvis March Convex Hull points : ");
for (int i = 0; i < next.length; i++)
{
if (next[i] != -1)
{
System.out.println("("+ points[i].x +", "+ points[i].y +")");
}
}
}
public static void main(String[] args) throws IOException {
int[] data=readfiles("nani.txt");
System.out.println(Arrays.toString(data));
}
public static int[] readfiles(String file)
{
try {
File f = new File(file);
Scanner scan = new Scanner(f);
int ctr = 0;
while (scan.hasNextInt()) {
ctr++;
System.out.println(scan.nextInt());
}
System.out.println("Jarvis Algorithm Test\n");
int[] arr = new int[ctr];
System.out.println(ctr);
scan.useDelimiter(",|\\s*");
/** Make an object of Jarvis class **/
int n = ctr;
for (int i = 0; i < n; i++) {
System.out.println(n);
Point[] points = new Point[n];
System.out.println("Reading X,Y Values From File");
points[i] = new Point();
points[i].x = arr[i];
points[i].y = arr[i + 1];
// arr[i]++;
i++;
System.out.println("(x,y) values are:" + points[i].x + "\t" + points[i].y);
}
O/P部分:这是我得到
输出运行:
1 2 6 4 8 7 2 3 12 13 Exception in thread "main" java.lang.NullPointerException 9 1 Jarvis Algorithm Test 12
阅读X,Y值从文件//错误我在jarvis.Jarvis.readfiles(Jarvis.java:118)获得 在jarvis.Jarvis.main(Jarvis.java:80) Java结果:1 BUILD SUCCESSFUL(总时间:0秒)
什么是你的问题,你的问题? – Abdelhak
即时尝试从文件读取vaalues ..存储在数组中的值int [] arr = new int [ctr];直到这里就OK了..接下来,数组值被输入到另一个方法,即points [i] = new point();这里i.m得到错误..这些数组值不读取到这个方法 –
尝试发布你的积分类 – Abdelhak