Results 1 to 3 of 3
  1. #1
    mxcnrawker is offline Member
    Join Date
    Apr 2012
    Location
    San Jose Ca
    Posts
    16
    Rep Power
    0

    Lightbulb Getting an Error on not initializing the variable in two methods

    I keep getting an error message when I compile my program in my two methods telling me that I didn't initialize the variables in the method.

    This is my code:

    Java Code:
    import java.util.Scanner;                // Needed to import the Scanner class.
    
    class TwoDimenArrayOperations 
    {
        public static void main(String[] args) 
        {
            // List of variables.
            int rows;                        // Declaring an integer for a variable named rows.
            int cols;                        // Declaring an integer for a varible named cols.
            double[][] array;            // Declaring a double for a two dimensional array named array.
            
            // Creating the Scanner object to read input.
            Scanner keyboard = new Scanner(System.in);
            
            // Gets the user to enter the number of rows desired.
            System.out.print("Enter the number of rows: ");
            rows = keyboard.nextInt();
            
            // Gets the user to enter th number of columns.
            System.out.print("Enter the number of columns: ");
            cols = keyboard.nextInt();
            
            // Redirects the users input for the 2 dimensional array.
            array = new double[rows][cols];
            
            // Creating a loop that will enter the data for the array.
            for (int r = 0; r < rows; r++)
            {
                // Another loop is created to enter the data for the matrix
                for (int c = 0; c < cols; c++) 
                {
                    // Gets the user to enter the data for the array.
                    System.out.print("Enter data for " + "[" + (r) + "]" + "[" + (c) + "]: ");
                    array[r][c] = keyboard.nextDouble();
                }
                
                // Prints out a blank line
                System.out.println();
            }
            
            /// List of methods:
            showMatrix(array);
            getTotal(array);
            getRowTotal(array);
            getColumnTotal(array);
            
            /*
            Displays the following methods:
                - Display the created matrix.
                - The total value of the matrix.
                - The total row value of the matrix.
                - The total column value of the matrix.
            */
            
            System.out.println("The total value of the matrix is: " + getTotal(array));
            System.out.println("The total row value of the matrix is: " + getRowTotal(array));
            System.out.println("Ther total column value of the matrix is: " + getColumnTotal(array));
            
            // Exits the program
            System.exit(0);
            
        }        // End of main program.
        
        // Creating a method named showMatrix.
        public static void showMatrix(double[][] tester)
        {
            // Creating a nested loop that will display the matrix.
            for (int r = 0; r < tester.length; r++)
            {
                for (int c = 0; c < tester[r].length; c++) 
                {
                    System.out.print(tester[r][c] + " ");
                    System.out.println();
                }
            }
        }
        
        // Creating a method named getTotal
        public static double getTotal(double[][] tester)
        {
            // List of local variables in the getTotal method.
            double total = 0;                    // Declaring a double for a variable named total, intialized at zero.
            
            // Creating a nested loop that will sum the total value of the matrix.
            for (int r = 0; r < tester.length; r++) 
            {
                for (int c = 0; c < tester[r].length; c++) 
                {
                    total += tester[r][c];
                }
            }
            
            // Returns the total value of the matrix.
            return total;
        }
        
     // Creating a methodo named getRowTotal.
    public static double getRowTotal(double[][] tester)
        {
            // List of local variables in the getRowTotal method.
             double totalRow;                    // Declaring a double for a variable named total.
            
            // Creating a nested loop that will calculate the total value of each individual row.
            // First loop is to change the poisition of the row.
            for (int r = 0; r < tester.length; r++) 
            {
                // Initializing the total to zero.
                totalRow = 0;
                
                // Loop created that sums the total amount in the row.
                for (int c = 0; c < tester[r].length; c++)
                {
                    totalRow += tester[r][c];
                }
            }
            
            // Returns the total row value of the matrix.
            return totalRow;    
        }
        
        // Creating a method named getColumnTotal.
        public static double getColumnTotal(double [][] tester)
        {
            // List of local variables in the getColumnTotal method.
            double totalCol;                        // Declaring a double for a variable named totalCol
            
            // Creating a nested loop that will calculate the total value in each individual column.
            // First loop is created to change the position of the column.
            for (int c = 0; c < tester[0].length; c++) 
            {
                // Initializing the total to zero
                totalCol = 0;
                
                // Calculates the total sum of the column.
                for (int r = 0; r < tester.length; r++) 
                {
                    totalCol += tester[r][c];
                }
            }
            
            // Returns the total column value of the matrix.
            return totalCol;
        }
    }
    My output says when I compile the program:

    TwoDimenArrayOperations.java:145: variable totalRow might not have been initialized
    return totalRow;
    ^
    TwoDimenArrayOperations.java:169: variable totalCol might not have been initialized
    return totalCol;
    ^
    2 errors

    Can anyone help me fix this problem?

  2. #2
    DeNiS_M is offline Member
    Join Date
    Sep 2011
    Location
    Athens Greece
    Posts
    29
    Rep Power
    0

    Default Re: Getting an Error on not initializing the variable in two methods

    double totalCol = 0.0 and double totalRow = 0.0 should fix the problem.
    Last edited by DeNiS_M; 04-07-2012 at 12:54 PM.

  3. #3
    mxcnrawker is offline Member
    Join Date
    Apr 2012
    Location
    San Jose Ca
    Posts
    16
    Rep Power
    0

    Default Re: Getting an Error on not initializing the variable in two methods

    Thanks a lot, I did that before but i took out the other initializer in the loop and got me the same as the whole total, but this fixed my problem.

Similar Threads

  1. Error initializing class instances
    By fr0s1yjack in forum New To Java
    Replies: 1
    Last Post: 06-22-2011, 04:58 PM
  2. Replies: 3
    Last Post: 05-05-2011, 04:33 PM
  3. Initializing a variable from another method
    By erikjd21 in forum New To Java
    Replies: 4
    Last Post: 04-17-2011, 09:55 AM
  4. Help with initializing error
    By GDW in forum New To Java
    Replies: 7
    Last Post: 10-20-2010, 09:10 PM
  5. same object variable t and same methods is calling
    By javastuden in forum New To Java
    Replies: 1
    Last Post: 11-24-2009, 05:10 AM

Tags for this Thread

Posting Permissions

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