Results 1 to 3 of 3
  1. #1
    mainy is offline Member
    Join Date
    Feb 2009
    Posts
    38
    Rep Power
    0

    Default How do I place these numbers into an array

    Hello,

    I am struggling with a fairly simple problem which I hope you can help with. I am working on a some code but there is a small part of it which I can't seem to get to work.

    I have two classes. I want to enter details into one, which will place those details into an array which can then be accessed.

    Class one:
    Java Code:
    public class Triangle {
    
            int x, y, l;
            int[] cx;
            int[] cy;
    
            public Triangle(int x, int y, int l) {
                this.x = x;
                this.y = y;
                this.l = l;
            }
    
            public void cords(){
               cy[0] = x;
               cx[1] = x+10;
               cx[2] = x+20;
    
               cy[0] = y;
               cy[1] = y+20;
               cy[2] = y;
    
        }
     }
    Then I have a class to test it:
    Java Code:
    public class Test {
    
        public static void main(String[] args) {
    
            Triangle tos = new Triangle(100, 200, 3);
    
            System.out.println(tos.cy[0]);
         
        }
    }
    But instead of getting the 200 print out, I get quite a few errors. Please help
    Last edited by mainy; 07-18-2009 at 04:15 PM.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Before you can use an array you must instantiate it:
    Java Code:
    int[] x;               // declaration
    int[] y = new int[4];  // declaration and instantiation
    Java Code:
    public class TriangleRx {
    
        int x, y, l;
        int[] cx;
        int[] cy;
    
        public TriangleRx(int x, int y, int l) {
            this.x = x;
            this.y = y;
            this.l = l;
            System.out.println("cx = " + cx + "  cy = " + cy);
        }
    
        public void cords() {
            cy[0] = x;
            cx[1] = x+10;
            cx[2] = x+20;
    
            cy[0] = y;
            cy[1] = y+20;
            cy[2] = y;
        }
    
        public static void main(String[] args) {
            TriangleRx test = new TriangleRx(2, 3, 5);
    //        test.cords();
        }
    }

  3. #3
    Harpreet1111 is offline Member
    Join Date
    Jul 2009
    Posts
    12
    Rep Power
    0

    Default

    u r using the array without instantiating it. just add

    cx=new int[size of array];
    cy=new int[]size of array];

    in constructor and the job is done......

Similar Threads

  1. Replies: 5
    Last Post: 02-07-2009, 07:48 AM
  2. Adding numbers in an array?
    By hawaiifiver in forum New To Java
    Replies: 9
    Last Post: 01-22-2009, 03:50 AM
  3. Adding numbers in a 2 dimensional array
    By j0shizabeast in forum New To Java
    Replies: 2
    Last Post: 11-27-2007, 04:31 AM
  4. Adding numbers in array
    By Shaolin in forum New To Java
    Replies: 1
    Last Post: 11-15-2007, 06:30 PM
  5. generating random numbers in a 5x5 array.
    By acidacid in forum New To Java
    Replies: 3
    Last Post: 08-14-2007, 03:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •