I have written a class to read a text file and write the data into a
2D array of max records x 12 fields. Now that I have this working, I need
to be ablel to call this array from my other classes to perform different
tasks on the data. Here is the code from the main class:

// Open the file to read
    File file = new File(filename);
    Scanner input = new Scanner(file);
    
    // create a Vehicle Records Array of
    // Rows [Max Records] x Columns [12]
    final int MAX = number++;
    String[][] VehicleData = new String[MAX][12]; 
    
    // read data from a file
    while (input.hasNext() & (i <= MAX)) 
    {   
        VehicleData[i][0] = input.next();    // Vehicle type
        VehicleData[i][1] = input.next();    // Driver last name field
        VehicleData[i][2] = input.next();    // Street address
        VehicleData[i][3] = input.next();    // City
        VehicleData[i][4] = input.next();    // State
        VehicleData[i][5] = input.next();    // Zip Code
        VehicleData[i][6] = input.next();    // Student ID
        VehicleData[i][7] = input.next();    // Car Make
        VehicleData[i][8] = input.next();    // Car Model
        VehicleData[i][9] = input.next();    // Car Year
        VehicleData[i][10] = input.next();   // Car VIN
        VehicleData[i][11] = input.next();   // State of car registration
        i++;
    }     
    // Close the file
    input.close();

   // The getData method accepts an array as an argument
   public static void getData(String[][] array)
   {
        return VehicleData;
   }

obviously something is wrong with my method to pass the array but I am a little lost!
Any and all help will be greatly appreciated!

Thanks!
Debi

Edited 4 Years Ago by peter_budo: Keep It Clear - Do use code tags when posting any code.

I guess you wanted something like this

public static void main(String[]args){
    String[][]vehicles data = getVehiclesData();
}

public static String[][] getVehiclesData(){
    // Open the file to read
    File file = new File(filename);
    Scanner input = new Scanner(file);
    
    // create a Vehicle Records Array of
    // Rows [Max Records] x Columns [12]
    final int MAX = number++;
    String[][] VehicleData = new String[MAX][12]; 
    
    // read data from a file
    while (input.hasNext() & (i <= MAX)) 
    {   
        VehicleData[i][0] = input.next();    // Vehicle type
        VehicleData[i][1] = input.next();    // Driver last name field
        VehicleData[i][2] = input.next();    // Street address
        VehicleData[i][3] = input.next();    // City
        VehicleData[i][4] = input.next();    // State
        VehicleData[i][5] = input.next();    // Zip Code
        VehicleData[i][6] = input.next();    // Student ID
        VehicleData[i][7] = input.next();    // Car Make
        VehicleData[i][8] = input.next();    // Car Model
        VehicleData[i][9] = input.next();    // Car Year
        VehicleData[i][10] = input.next();   // Car VIN
        VehicleData[i][11] = input.next();   // State of car registration
        i++;
    }     
    // Close the file
    input.close();
  
        return VehicleData;
}

This generates the error cannot return a value from a method whose result type is void

ok, this is what is required... Driver is the main class from this class I read the data and create the 2d array. I must pass the array to another class called person where several of the fields are printed in a report, I placed the pass in the loop so it would print the report as it loads the array... then pass to vehicle which prints more fields .. then passed to registration which sums the total cost based on vehicle type. After all is done the final total is printed.

I have all of the classes set up but of course the passing of the array is not working too well the first error starts where the astericks are below:

public static String[][] getVehicleData()throws IOException
    {
    registration reg;     // Link the Registration class
    person peep;          // Link the Person class
    vehicle trans;        // Link the Vehicle class
    
    int i = 0;            // Array Counter      
    
    // Create a scanner object for keyboard input
    Scanner keyboard = new Scanner(System.in);
    
    // Get the file name
    System.out.print("Enter the number of records in your file :");
    int number = keyboard.nextInt();
    System.out.print("Enter the filename : ");
    String filename = keyboard.next();
    
    // Open the file to read
    File file = new File(filename);
    Scanner input = new Scanner(file);
    
    // create a Vehicle Records Array of
    // Rows [Max Records] x Columns [12]
    final int MAX = number++;
    String[][] VehicleData = new String[MAX][12]; 
    
    reg.getHeader(); 
    
    // read data from a file
    while (input.hasNext() & (i <= MAX)) 
    {   
        VehicleData[i][0] = input.next();    // Vehicle type
        VehicleData[i][1] = input.next();    // Driver last name field
        VehicleData[i][2] = input.next();    // Street address
        VehicleData[i][3] = input.next();    // City
        VehicleData[i][4] = input.next();    // State
        VehicleData[i][5] = input.next();    // Zip Code
        VehicleData[i][6] = input.next();    // Student ID
        VehicleData[i][7] = input.next();    // Car Make
        VehicleData[i][8] = input.next();    // Car Model
        VehicleData[i][9] = input.next();   // Car Year
        VehicleData[i][10] = input.next();   // Car VIN
        VehicleData[i][11] = input.next();   // State of car registration
     ***********pass DATA HERE ******************
        peep.getPersonData(VehicleData[][] int i); 
        trans.getTransportData();
        totalCost = (totalCost + trans.getCost());        
    }     
    input.close();                    // Close the text file
    return VehicleData;               // Return the VehicleData 
   }
}

Thanks again!
Debi

Edited 4 Years Ago by peter_budo: Keep It Clear - Do use code tags when posting any code

My wild guess given that we have no idea of the rest of your program is that

peep.getPersonData(VehicleData[][] int i); 
        trans.getTransportData();
        totalCost = (totalCost + trans.getCost());

should be used in other method or in main method where you use/need totalCost

the first error starts

Please post the full text of the error messages.

peep.getPersonData(VehicleData[][] int i);

One problem I see is the syntax of this method call. You should use the names of variables as arguments to the method, not the datatypes.

This article has been dead for over six months. Start a new discussion instead.