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

Thread: 1st time using methods

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    28
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 1st time using methods

        public static int loadMat(int [][] mat) throws Exception{
            int size, row, col = 0, num;
            Scanner input = new Scanner(System.in);
                System.out.print("Enter the size of the square : ");
                    size = input.nextInt();
                    mat = new int [size][size];
    System.out.println();
    System.out.println("Enter the value for");
        for (row = 0 ; row < size ; row++){
            for (col = 0 ; col < size ; col++){
                System.out.print("ROW " + row +" COL " +col +" : ");
                    num = input.nextInt();
                    mat[row][col] = num;
        }
    }
    return size;
        }
     
    public static void printMat(int [][] mat, int size){
         for (int i = 0 ; i < size ; i++){
             System.out.print("ROW " + i +" ");
                     for (int j = 0 ;j < size; j++){
                         System.out.print(size);
                     }
         }
     }
     
     
     
    public static void main(String[] args) throws Exception{
    int [][] themat = new int [10][10];
    int thesize;
    int magicnum;
    boolean row,col,diag;
    thesize = loadMat(themat);
    printMat(themat, thesize);
     
    }
    }

    so far i have that done...
    which prompts the user to fill in the matrix... what i dont get is in the method printMat
    how can i get a hold of the numbers the user just entered for the matrix?


  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: 1st time using methods

    how can i get a hold of the numbers the user just entered for the matrix?
    You've passed the array as a parameter, so just loop through the array by row/colum
    for ( int i = 0; i < mat.length; i++ ){
        for ( int j = 0; j < mat[i].length; j++ ){
          //do something with mat[i][j]
        }
    }

  3. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    28
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: 1st time using methods

    i already tried that code

     public static void printMat(int [][] mat, int size){
        for ( int i = 0; i < mat.length; i++ ){
        for ( int j = 0; j < mat[i].length; j++ ){
         System.out.println(mat[i][j]);
        }
    }
     }

    but the value of mat[i][j] is 0 which never changed even if the user inputed diff. values.. heres my whole code
    still dont know how to access the inputted values.


       public static int loadMat(int [][] mat) throws Exception{
            int size, row, col = 0, num;
            Scanner input = new Scanner(System.in);
                System.out.print("Enter the size of the square : ");
                    size = input.nextInt();
                    mat = new int [size][size];
    System.out.println();
    System.out.println("Enter the value for");
        for (row = 0 ; row < size ; row++){
            for (col = 0 ; col < size ; col++){
                System.out.print("ROW " + row +" COL " +col +" : ");
                    num = input.nextInt();
                    mat[row][col] = num;
        }
    }
    return size;
        }
     
     
     public static void printMat(int [][] mat, int size){
        for ( int i = 0; i < mat.length; i++ ){
        for ( int j = 0; j < mat.length; j++ ){
         System.out.println(mat[i][j]);
        }
    }
     }
     
     
     
    public static void main(String[] args) throws Exception{
    int [][] themat = new int [10][10];
    int thesize;
    int magicnum;
    boolean row,col,diag;
    thesize = loadMat(themat);
    printMat(themat, thesize);
     
    }
    }

  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: 1st time using methods

    Didn't spot this earlier (obviously), but you re-assign the variable mat in the load method, once the load method exits that variable is out of scope (and the parameter unchanged - pass by value ). I'd recommend returning the array from the load method, then send that into the print method. Lastly, take a look at the print method because it doesn't fully implement my suggestion above.

  5. #5
    Junior Member
    Join Date
    Nov 2010
    Posts
    28
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: 1st time using methods

    yeah but my homework specifically said that the data should be stored in the reference variable called mat and the size of the method should be the value returned

    uhh you said to return the array in the load method but as far as i know you can only return one value i think

  6. #6
    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: 1st time using methods

    Well technically the size is returned with the array (via the length property). But regardless, you cannot recreate the reference to the array in the method and hope that change takes effect outside of the method. Return the array, create an object which contains the array as an instance variable and pass that through the functions, or create 2 functions, one which asks for the size, create the array using that value, and pass that created array to another function that fills in the array.

Similar Threads

  1. Using time
    By ellias2007 in forum Java Theory & Questions
    Replies: 1
    Last Post: September 4th, 2010, 08:48 AM
  2. how to make a program take time...
    By DLH112 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 10th, 2010, 07:09 PM
  3. [SOLVED] frame are exiting at the same time
    By chronoz13 in forum AWT / Java Swing
    Replies: 8
    Last Post: January 25th, 2010, 08:13 PM
  4. display time
    By kalees in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: January 1st, 2010, 07:40 AM
  5. SQL Time Calculation
    By r12ki in forum JDBC & Databases
    Replies: 3
    Last Post: July 31st, 2009, 03:54 AM