0

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; 
1
  • 1
    I dont see anywhere your reading or writing to a file. Commented Nov 15, 2013 at 23:58

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.