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 4 of 4

Thread: Why cant I call on the array I returned in the main method?

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Why cant I call on the array I returned in the main method?

    //matrix1
       double[][] matrix1 = {{1, 2, 3},{4, 5, 6},{7, 8, 9}};      
     
    //matrix2
       double[][] matrix2 = {{0, 2, 4},{1, 4.5, 2.2},{1.1, 4.3, 5.2}};   
     
       addMatrix(matrix1, matrix2);
     
       printArrays(matrix1, matrix2, result, '+');
     
     
      }//end of main
     
        public static double[][] addMatrix(double[][] a, double[][] b){
     
    double[][] result = {{a[0][0]+b[0][0], a[0][1]+b[0][1], a[0][2]+b[0][2]},
        {a[1][0]+b[1][0], a[1][1]+b[1][1], a[1][2]+b[1][2]},
        {a[2][0]+b[2][0], a[2][1]+b[2][1], a[2][2]+b[2][2]}};
     
    return result;
    }
     
        public static void printArrays(double[][] m1, double[][] m2, double[][] m3, char op)
    {
    for (int i = 0; i < m1.length; i++) {
    for (int j = 0; j < m1[0].length; j++) {
    System.out.print(" " + m1[i][j]);
    }
    if (i == m1.length / 2)
    System.out.print(" " + op + " ");
    else {
    System.out.print(" ");
    }
    for (int j = 0; j < m2[0].length; j++) {
    System.out.print(" " + m2[i][j]);
    }
    if (i == m1.length / 2)
    System.out.print(" = ");
    else {
    System.out.print(" ");
    }
    for (int j = 0; j < m3[0].length; j++) {
    System.out.print(" " + m3[i][j]);
    }
    System.out.println();
    }
    } 
    //    public static void printArrays(double[][] a) {                        
    //        for(int row = 0; row < a.length; row++){
    //        for (int col = 0; col < a[row].length; col++) {
    //            
    //            System.out.print(a[row][col] + " ");
    //        }//end of inner for loop
    //        System.out.println("");
    //        
    //}//end of outer for loop
    //    }//end of printArrays
     
        }//end of class


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Why cant I call on the array I returned in the main method?

    See the link in my signature entitled "Getting Help". I might be the only one, but have no clue what you are asking - the guidelines in that link will help you get your point/question across.

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Why cant I call on the array I returned in the main method?

    In my method addMatrix I return the array result. I then try to call it in my main method to print out, however it gives me an error saying it cannot find the variable. Since I returned it shouldnt I be able to call it?

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Why cant I call on the array I returned in the main method?

    Quote Originally Posted by ColeTrain View Post
    In my method addMatrix I return the array result. I then try to call it in my main method to print out, however it gives me an error saying it cannot find the variable. Since I returned it shouldnt I be able to call it?
    The method may have returned the matrix, but when you call the method you do not actually assign a reference to the returned value - the reference within the method being out of scope once the method returns. You must first assign a reference value to the returned value of the method before you can use it.

Similar Threads

  1. [SOLVED] Method works, value not returned.
    By SPACE MONKEY in forum Object Oriented Programming
    Replies: 2
    Last Post: April 29th, 2012, 09:23 AM
  2. How to call a value from a different method to main
    By CrimsonFlash in forum What's Wrong With My Code?
    Replies: 13
    Last Post: October 22nd, 2011, 06:29 PM
  3. How do I call a method from the main method?
    By JavaStudent1988 in forum Java Theory & Questions
    Replies: 5
    Last Post: October 19th, 2011, 08:37 PM
  4. Can i call init() method in destroy method.?
    By muralidhar in forum Java Servlet
    Replies: 1
    Last Post: October 22nd, 2010, 11:18 AM
  5. how to reuse method returned string ??
    By zeeshanmirza in forum Collections and Generics
    Replies: 7
    Last Post: September 4th, 2009, 03:54 PM