Reputation: 43
I've been trying to fill an array with strings & break it into different arrays, but i must not be reading it in right because my array has nothing in it?
File inf = FileUtil.openFile(args); // open the file passedin in args
Scanner fin = new Scanner(inf);
public static File readFileInfo(Scanner kb)throws FileNotFoundException
{
String fileName = null;
File inFile = null;
do
{
System.out.print("Enter the name of the input file: ");
fileName = kb.nextLine();
inFile = new File(fileName);
}while(!inFile.exists());
return inFile;
}// end readFileInfo
public static Author[] fillArray(Scanner fin)
{
Author[] array = new Author[100];
String first_ = null;
String last_ = null;
String publisher_ = null;
int x = 0;
while(fin.hasNext())
{
first_ = fin.nextLine();
last_ = fin.nextLine();
publisher_ = fin.nextLine();
Author temp = new Author(first_, last_, publisher_);
array[x] = temp;
x++;
}
return array;
Upvotes: 0
Views: 850
Reputation: 5637
Maybe the problem is that you check if you have one more token but try to fetch three tokens
while(fin.hasNext()) // CHECK IF WE HAVE ONE MORE TOKEN
{
first_ = fin.nextLine(); // FETCH THREE
last_ = fin.nextLine();
publisher_ = fin.nextLine();
Author temp = new Author(first_, last_, publisher_);
array[x] = temp;
x++;
}
This may have lead to an exception, and the empty array.
Upvotes: 2