Java - 将 .txt 文件读取到 arrayList 会忽略最后一次迭代并显示错误
Java - reading .txt file to arrayList omits last iteration and shows error
我有一个 .txt 文件,需要将其内容放入数组列表中。
它采用以下格式,每个元素(int,String)在文档中换行。
整数零件数
字符串部分名称
字符串描述
国际价格
字符串部分名称
字符串描述
国际价格
等等
每当我 运行 我的程序时,它都会添加第一个属性,但不会将最后三个属性添加到数组列表中。
public static void loadPartsCleanly(){
try {
// java.io.File file = new java.io.File("parts.txt");
Scanner in = new Scanner(new File("parts.txt"));
ArrayList<Part> partlist = new ArrayList<Part>();
String name=null;
String description = null;
double price =0.0;
int totalParts = 0;
totalParts = in.nextInt();
in.nextLine();
//totalParts ++ ;
System.out.println(totalParts);
for (int i = 0; i < totalParts; i++)
{
//ArrayList<Part> partlist = new ArrayList<Part>();
name = in.nextLine();
description = in.nextLine();
price = in.nextDouble();
in.nextLine();
int quantityInStock = 5;
partlist.add(new Part(name, description, price, quantityInStock));
System.out.println(partlist);
}
in.close();
} catch (FileNotFoundException fnfe) {
System.out.println("Unable to locate the parts.txt file for opening.");
} catch (Exception otherExc) {
System.out.println("***** An unexpected error has occurred *****");
otherExc.printStackTrace();
}
}
所以在上面的代码中,它读取文本文档中的第一个 Int 并将其分配给 for 循环。
totalParts = in.nextInt();
in.nextLine();
//totalParts ++ ;
System.out.println(totalParts);
for (int i = 0; i < totalParts; i++)
无论 totalParts 是 8 还是 20,循环工作正常,直到需要将最后一部分添加到数组列表。
出现这个错误..
An unexpected error has occurred java.util.NoSuchElementException: No
line found
我一直在努力解决这个问题,但越来越多的挫败感促使我 post 在这里,所以任何正确方向的指示都将不胜感激。如果您需要澄清有关我的问题的任何内容,请询问。
nextInt()
和 nextDouble()
方法与 nextLine()
不同。 nextLine()
读取 \n 但其他的。
因此,如果每个元素都在不同的行中,则应始终使用 nextLine()
,然后将该行解析为所需的类型,例如:
String line = in.nextLine();
double price = Double.parseDouble(line);
我认为你输入文件的最后一行是一个数字。
我从您的代码中看到的是,您使用 nextDouble
读取价格,然后使用 nextLine
转到下一行。
在这种情况下,如果最后一个数字后面没有更多的行,你就会出错。
以下代码可以解决您的问题。
if (i + 1 < totalParts) {
in.nextLine();
}
我有一个 .txt 文件,需要将其内容放入数组列表中。 它采用以下格式,每个元素(int,String)在文档中换行。
整数零件数 字符串部分名称 字符串描述 国际价格 字符串部分名称 字符串描述 国际价格 等等
每当我 运行 我的程序时,它都会添加第一个属性,但不会将最后三个属性添加到数组列表中。
public static void loadPartsCleanly(){
try {
// java.io.File file = new java.io.File("parts.txt");
Scanner in = new Scanner(new File("parts.txt"));
ArrayList<Part> partlist = new ArrayList<Part>();
String name=null;
String description = null;
double price =0.0;
int totalParts = 0;
totalParts = in.nextInt();
in.nextLine();
//totalParts ++ ;
System.out.println(totalParts);
for (int i = 0; i < totalParts; i++)
{
//ArrayList<Part> partlist = new ArrayList<Part>();
name = in.nextLine();
description = in.nextLine();
price = in.nextDouble();
in.nextLine();
int quantityInStock = 5;
partlist.add(new Part(name, description, price, quantityInStock));
System.out.println(partlist);
}
in.close();
} catch (FileNotFoundException fnfe) {
System.out.println("Unable to locate the parts.txt file for opening.");
} catch (Exception otherExc) {
System.out.println("***** An unexpected error has occurred *****");
otherExc.printStackTrace();
}
}
所以在上面的代码中,它读取文本文档中的第一个 Int 并将其分配给 for 循环。
totalParts = in.nextInt();
in.nextLine();
//totalParts ++ ;
System.out.println(totalParts);
for (int i = 0; i < totalParts; i++)
无论 totalParts 是 8 还是 20,循环工作正常,直到需要将最后一部分添加到数组列表。
出现这个错误..
An unexpected error has occurred java.util.NoSuchElementException: No line found
我一直在努力解决这个问题,但越来越多的挫败感促使我 post 在这里,所以任何正确方向的指示都将不胜感激。如果您需要澄清有关我的问题的任何内容,请询问。
nextInt()
和 nextDouble()
方法与 nextLine()
不同。 nextLine()
读取 \n 但其他的。
因此,如果每个元素都在不同的行中,则应始终使用 nextLine()
,然后将该行解析为所需的类型,例如:
String line = in.nextLine();
double price = Double.parseDouble(line);
我认为你输入文件的最后一行是一个数字。
我从您的代码中看到的是,您使用 nextDouble
读取价格,然后使用 nextLine
转到下一行。
在这种情况下,如果最后一个数字后面没有更多的行,你就会出错。
以下代码可以解决您的问题。
if (i + 1 < totalParts) {
in.nextLine();
}