1

I have 2 arrays namely x and y and I wish to convert them into one Point array.

This is what I've tried, but it doesn't seem to be working.

public class Point {
     public static void main(String[] args) throws Exception {
         Point[] objectPoints;
         double x[] = {3,4,5};
         double y[] = {4,5,6};

         for (int i = 0;i < 2; i++) {
             objectPoints[i] = new Point(x[i],y[i]);
         }
     }
}

How do I achieve that?

marcospereira
  • 12,045
  • 3
  • 46
  • 52
Cael
  • 556
  • 1
  • 10
  • 36
  • 3
    _"Doesn't seem to be working"_ is insufficient. Describe exactly what you're seeing, what you expect, and why you think there's a problem. If there is an error message or stack trace, copy/paste the information into your post (format as code for readability). Without that basic information nobody can help you. – Jim Garrison Feb 20 '16 at 17:36
  • 2
    You are calling a constructor that does not exist. – Frank Puffer Feb 20 '16 at 17:38
  • 1
    Also read http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it – JB Nizet Feb 20 '16 at 17:38
  • 1
    I don't see where you initialize your objectPoints array – Solorad Feb 20 '16 at 17:39

7 Answers7

1

You haven't said what you think is wrong, but here are some obvious issues:

You haven't defined a constructor for Point, or any instance variables in Point to hold the x and y coordinates, and

for (int i=0;i<2;i++)

should be

for (int i=0;i<x.length;i++)

You're headed in the right direction but you're missing some basics.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
0

So, finally your code should look something like that:

class Point {
    private double x;
    private double y;

    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public static void main(String[] args) throws Exception {

        double x[] = {3, 4, 5};
        double y[] = {4, 5, 6};
        Point[] objectPoints = new Point[x.length];

        for (int i = 0; i < 2; i++) {
            objectPoints[i] = new Point(x[i], y[i]);
        }

    }
}
Solorad
  • 904
  • 9
  • 21
0
  1. Your Point type array should have a size to create indexes for the values to be inserted, the size of array should be known.
  2. You can only pass int values to Point constructor while you are passing double values.
  3. Dont use a class name Point because you are Using the builtin java Point in your program.

    public class ThePoint {
    
    public static void main(String[] args) throws Exception {
        double x[] = {3, 4, 5};
        double y[] = {4, 5, 6};
        Point[] objectPoints = new Point[x.length];
        for (int i = 0; i < x.length; i++) {
            objectPoints[i] = new Point((int) x[i], (int) y[i]);
        }
        System.out.println(objectPoints[0].x);
      }
    }
    
Muhammad
  • 6,725
  • 5
  • 47
  • 54
0

Your Point class doesn't have a constructor, your objectPoints array is never initialized and your loop only goes from 0 to 1. What you want is something like this:

public class Point {

    private double x, y;

    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public static void main(String[] args) {
        Point[] objectPoints = new Point[3];
        double x[] = {3,4,5};
        double y[] = {4,5,6};

        for (int i = 0; i < objectPoints.length; i++) {
            objectPoints[i] = new Point(x[i], y[i]);
        }
    }

}
pp_
  • 3,435
  • 4
  • 19
  • 27
0

You need to check whether the 2 arrays have the same length or not. Secondly, you can't name your project Point since this name is kind of used by the Point existing class.

public class MyPoint {
    public static void main(String[] args) throws Exception {

    Point[] objectPoints;
    double x[] = {3,4,5};
    double y[] = {4,5,6};

    if(x.length != y.length)
        throw new //MyTypeError
    else//the x and y array share the same length
    {
        objectPoints = new Point[x.length];
        for (int i=0; i < x.length; i ++)
        {
            objectPoints[i] = new Point(x[i],y[i]);
        }
    }
}
Simply Me
  • 1,579
  • 11
  • 23
0

You shouldn't use array with output you need to use ArrayList. And I think you shouldn't use class name Point that would cause some troubles in functions of the class

import java.lang.*;
/***********************************/
ArrayList<Point> objectPoints = new ArrayList<Point>();
double x[] = {3,4,5};
double y[] = {4,5,6};
if (!(x.Length == y.Length))
    // YOU SHOULD DO THIS EVEN IF YOU'RE SURE THAT THEY ARE THE SAME
for (int i = 0;i < x.Length; i++) {
    objectPoints.Add(new Point((int)x[i],(int)y[i]));
}

Sorry I don't remember if it was add or insert because I'm working with a lot of languages at the same time :v :v

Ziad Alzarka
  • 333
  • 1
  • 2
  • 12
-1
public class Point {
  //you forgot variables to hold values
  private double x;
  private double y;

  //you forgot constructor
  public Point (double x, double y) {
    this.x = x;
    this.y = y;
  }
  public static void main(String[] args) throws Exception {
    double x[] = {3, 4, 5};
    double y[] = {4, 5, 6};

    //just a check that x and y size should be equal
    if (x.length != y.length) {
      throw new Exception("points x array is not equal to points y array");
    }

    Point[] objectPoints = new Point[x.length];
    for (int i = 0; i < objectPoints.length; i++) {
      objectPoints[i] = new Point(x[i], y[i]);
    }
  }
}
ernestk
  • 72
  • 6