2016-02-14 46 views
-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秒)

+2

什么是你的问题,你的问题? – Abdelhak

+0

即时尝试从文件读取vaalues ..存储在数组中的值int [] arr = new int [ctr];直到这里就OK了..接下来,数组值被输入到另一个方法,即points [i] = new point();这里i.m得到错误..这些数组值不读取到这个方法 –

+0

尝试发布你的积分类 – Abdelhak

回答

0

你想从中读取数据的数组未初始化这就是为什么你会得到一个空指针异常。如果您不知道将要保存阵列的元素的数量,则使用ArrayList将会很合适。这是你的readFiles方法应该是什么样子:

public static int[] readfiles(String file) 
{ 

try { 
    File f = new File(file); 
    Scanner scan = new Scanner(f); 
    List<Integer> arr = new ArrayList<>(); 
    int crr = 0; 

    while(scan.hasNextInt()) 
    { 
     arr.add(scan.nextInt()); 
     System.out.println(arr.get(crr++)); 
    } 

    System.out.println("Jarvis Algorithm Test\n"); 

    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.get(i); 
     points[i].y = arr.get(i + 1); 

     i++; 
     System.out.println("(x,y) values are:" + points[i].x + "\t" + points[i].y); 
    } 

如果你想一个ArrayList转换成数组:

TypeOfTheArray array = new TypeOfTheArray [arrayList.size()]; 

for(int i = 0; i < array.length; i++) 
    array[i] = arrayList.get(i); 

// It's important to clean up the trash :) 
arrayList.clear();