Filling arrays from an input file?
Hi, on my previous homework we were to read a given file and fill an array with the entire contents, which was fine.
However now we need to create separate arrays from each line in the text file. For example, my input file, teams.txt is:
4
Mariners RedSox Yankees Cubs
Lakers Knicks Bulls Cavaliers
Cowboys Falcons Vikings Packers
4 corresponds to the number of teams
Line one consists of baseball teams
Line two of basketball teams
Line three of football teams
How can I create baseballArray of the baseball teams, basketArray of basketball teams, and so forth?
In the previous homework we used this code to fill the entire array, working with students and ID#s:
Code:
public static void fillArray(Student [] myClass, Scanner fin)
{
for(int i = 0; i < myClass.length; i++)
{
String n = fin.nextLine();
int id = Integer.parseInt(fin.nextLine());
Student temp = new Student (n, id);
myClass[i] = temp;
}
}// end fillArray
any clues would be greatly appreciated as always!
this is what I have for the time being, emulating what we did in the previous assignment, but gives me a no line found error:
Code:
public static void fillArray(String [] myArray, Scanner fin)
{
for(int i = 0; i < myArray.length; i++)
{
int num = Integer.parseInt(fin.nextLine());
String bb = fin.nextLine();
String basetemp = new String (bb);
myArray[i] = basetemp;
String bkb = fin.nextLine();
String baskettemp = new String (bkb);
myArray[i] = baskettemp;
String f = fin.nextLine();
String foottemp = new String (f);
myArray[i] = foottemp;
}
}// end fillArray