gpt4 book ai didi

java - 从命令行填充数组txt.file java

转载 作者:行者123 更新时间:2023-12-02 06:31:38 25 4
gpt4 key购买 nike

我一直在尝试用字符串填充一个数组并将其分成不同的数组,但我一定不能正确读取它,因为我的数组中没有任何内容?

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;

最佳答案

也许问题在于您检查是否还有一个 token ,但尝试获取三个 token

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++;
}

这可能会导致异常和空数组。

关于java - 从命令行填充数组txt.file java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20012741/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com