简体   繁体   中英

filling array from command line txt.file java

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; 

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM