从整数数组中读取X,Y值

时间:2016-02-14 03:36:31

标签: java filereader

我试图从整数数组中读取x,y值  我的代码:

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 Part:这是我得到的输出

     

运行:

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值//错误我得到了           at jarvis.Jarvis.readfiles(Jarvis.java:118)           at jarvis.Jarvis.main(Jarvis.java:80)       Java结果:1       建立成功(总时间:0秒)

1 个答案:

答案 0 :(得分:0)

您尝试读取的数组未初始化,这就是您获得NullPointerException的原因。如果您不知道将保存阵列的数量,请使用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();