I'm trying to practice reading text files into a multidimensional object array, and I can't seem to figure out how to do that. The text file I have contains the days of the week, the number of flights for that day and their destinations, and accompanying info. I will be using another text file with varying number of flights, so I don't want to hard-code the second dimension.
Here is the text file info:
Monday 3
Paris 6 200
Munich 3 88
Athens 4 190
Tuesday 1
Paris 6 230
Wednesday 2
Paris 6 235
London 2 58
Thursday 3
Paris 6 210
Beijing 3 120
Munich 3 100
Friday 1
Paris 7 300
Saturday 0
Sunday 2
Paris 2 50
Victoria 1 110
The code I have so far is:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Arrays;
public class Airlines {
/**
* read txt file(s), output data pertaining to weekly dining report for airline
*
*/
public static void main( String[] args ) throws FileNotFoundException {
String inputFile = "flights.txt";
//read input file and create a ragged array with flights for each day of the week
Airplane[][] flightsThisWeek = readData(inputFile);
//Ensure data is read in properly
testArray(flightsThisWeek);
//System.out.println("This week's report (input file " + inputFile + ")\n");
}
/** readData - method called from the main with the input file name
* this methods reads the file into a two dimensional ragged array of Airplane objects
*
* @param String file - name of the file containing the input data
*
* @returns Airplane[][]
*/
private static Airplane[][] readData(String file) {
Aprplane[][] planeArr = new Airplane[7][];
try {
Scanner scan = new Scanner (new File(file));
while (scan.hasNextLine()) {
for (int i = 0; i < planeArr.length; i++) {
String readLine = scan.nextLine();
String[] line = readLine.split(" ");
int numPlanes = Integer.parseInt(line[1]);
for (int j = 0; j < numTrains; j++) {
String readNewLine = scan.nextLine();
String[] newLine = readNewLine.split(" ");
String city = newLine[0];
int carts = Integer.parseInt(newLine[1]);
int sold = Integer.parseInt(newLine[2]);
Airplane plane2 = new Airplane(city, cars, sold);
}
Airplane[][] newPlane = new Airplane[i][numPlanes];
planeArr = newPlane;
}
}
}
catch (Exception e) {
System.out.println("File not found");
}
return trainArr;
}
/** testArray - test for correctness
*
* @param Airplane[][] a - the 2D jagged array of Airplane objects
*
* @returns nothing
*/
private static void testArray(Airplane[][] a) {
for ( int i = 0; i < 7; i++ )
System.out.println(Arrays.toString(a[i]));
}
/**
* toString - method to print an Airplane object in the form
* destination/planes/passenger capacity/dining carts/stocked/sold
* @param none
* @returns String
*/
public String toString() {
return "To " + this.destination + " " + this.planes + "/" + this.passengerCapacity + "/" +
this.diningCarts + "/" + this.mealsStocked + "/" + this.mealsSold;
}
}
Basically, what I'm trying to do is use the number of planes (following the day of the week) to create the second dimension of the array, and use the plane info following that to sort into their own buckets, and print the array without the day of the week and the number of planes, so:
[To destination and info, To destination and info, To destination and info] (3 Monday flights)
[To destination and info] (1 Tuesday flight) etc.
I'm sure I have to do something to iterate through the readData method, but I'm not quite sure how to accomplish that. Any help would be greatly appreciated!