【问题标题】:Reading X,Y Values from integer array从整数数组中读取 X,Y 值
【发布时间】:2016-02-14 03:36:30
【问题描述】:

我正在尝试从整数数组中读取 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 部分:这是我得到的输出

运行:

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 构建成功(总时间:0 秒)

【问题讨论】:

  • 你的问题和问题是什么?
  • 我试图从文件中读取值 .. 值存储在数组 int[] arr=new int[ctr];到这里没关系..接下来将数组值输入到另一个方法,即 points[i]=new point();这里我得到错误..那些数组值没有被读取到这个方法
  • 尝试发布您的积分课程
  • 请不要试图在 cmets 中解释您的问题。您应该编辑您的问题以包含所有重要数据,例如您遇到的任何错误的完整文本。
  • 将您的错误/异常添加到您的帖子中(使用edit)。包括完整的堆栈跟踪。

标签: java filereader


【解决方案1】:

您尝试读取的数组未初始化,这就是您收到 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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-30
    • 2020-04-29
    相关资源
    最近更新 更多