Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Problem with return and 2D array

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with return and 2D array

    Hello, first time on a programming forum, hopefully this is in the right place and I inserted the code correctly. Anyway, I am having trouble figuring out what the problem is in the code below. I return the array and in my next line of my main method when I try to use the returned array "pointArray" I am getting an error that says "cannot find symbol". Am I misunderstanding how the return statement should return? Been trying things for the last few hours and I haven't found a solution. Still pretty new to programming, trying to learn but I don't know what else to do debugging wise.

    I can post my other class if necessary but I didn't think it was. Let me know.

    Thanks

    public class ManyCircles {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            randomPoints(5);
            //averagePoint(pointArray);
     
            printPoints(pointArray);  //After I return from randomPoints I get a "Cannot find symbol" error here for pointArray
        }
     
        public static Point1080[][] randomPoints(int n) {
            int x;
            int y;
            //Declares 2D array
            Point1080[][] pointArray = new Point1080[n][2];
            //Goes through n elements of the array        
            for (int i = 0; i < pointArray.length; i++) {
                //For n element, give x and y values in 2nd array
                for (int j = 0; j < 2; j++) {
                    x = (int) ((int) 1920 * Math.random());
                    y = (int) ((int) 1080 * Math.random());
                    pointArray[i][j] = new Point1080(x, y);
                }
                System.out.println("X " + i + " is: " + pointArray[i][0].getX());
                System.out.println("Y " + i + " is: " + pointArray[i][1].getY());
     
            }
            //Should return the 2D array, not returning it to main method though
            return pointArray;
        }
     
        private static void printPoints(Point1080[] V) {
            for (Point1080 E : V) {
                System.out.format("(%4d,%4d)\n", E.getX(), E.getY());
            }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,469
    My Mood
    Grumpy
    Thanks
    0
    Thanked 159 Times in 150 Posts

    Default Re: Problem with return and 2D array

    Your code may have returned the array but it does nothing with it and it disappears into the ether.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with return and 2D array

    Oh, so do I need to re-declare the array? I want to use the array later. This 2D array stuff is confusing me some. I am trying to think of it like I am just returning an integer or string. I am putting a random value in the second part of the array (don't know what you call it).

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,469
    My Mood
    Grumpy
    Thanks
    0
    Thanked 159 Times in 150 Posts

    Default Re: Problem with return and 2D array

    No. All you need to do is assign the returned array to a variable.
    Improving the world one idiot at a time!

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with return and 2D array

    Ooooooooohhh...I see. Thanks for the help. Now I get what you meant by does nothing with it.

Similar Threads

  1. How do I return an array list?
    By 93tomh in forum Java Theory & Questions
    Replies: 2
    Last Post: July 30th, 2012, 04:03 PM
  2. Doubling The Array Size And Randomizing Array Return
    By Pingu00 in forum What's Wrong With My Code?
    Replies: 18
    Last Post: June 27th, 2011, 10:50 AM
  3. Search for number in array and return index
    By Kevinius in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 24th, 2011, 12:00 AM
  4. [SOLVED] Problem accessing specific data in an array and getting it to return properly
    By Universalsoldja in forum Collections and Generics
    Replies: 3
    Last Post: February 4th, 2010, 03:26 PM
  5. Error of missing return statement while implementing Array
    By MS_Dark in forum Collections and Generics
    Replies: 1
    Last Post: December 10th, 2008, 02:18 PM