-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