【发布时间】:2018-11-29 17:08:41
【问题描述】:
我正在尝试读取包含创建手机的属性(例如序列号、品牌、年份和价格)的文件。然后,我想将信息存储在变量中,以便可以使用其构造函数创建 CellPhone 对象。之后我需要继续在链接列表中添加这些 CellPhone 对象,同时确保没有重复项(具有相同序列号的 CellPhone 对象)。它适用于 List 为空的第一种情况,但是在我将第一个对象添加到 List 之后,就会出现 NoSuchElementException。我做错了什么,如何正确读取文件?任何帮助表示赞赏。
CellListUtilization 类:
// Method to read the file and store the information in the CellList
public static void processFile(Scanner sc1, CellList cl1) {
String S = null;
while(sc1.hasNext())
{
// First case where the list is empty
if (cl1.getSize() == 0)
{
S = sc1.next();
serialNum = Long.parseLong(S);
S = sc1.next();
brand = S;
S = sc1.next();
price = Double.parseDouble(S);
S = sc1.next();
year = Integer.parseInt(S);
CellPhone c1 = new CellPhone(serialNum, brand, year, price);
cl1.addToStart(c1);
}
else
{
serialNum = Long.parseLong(S);
S = sc1.next();
brand = S;
S = sc1.next();
price = Double.parseDouble(S);
S = sc1.next();
year = Integer.parseInt(S);
if (!(cl1.contains(serialNum)))
{
CellPhone c2 = new CellPhone(serialNum, brand, year, price);
cl1.addToStart(c2);
}
}
S = sc1.next();
}
}
我要读取的文件:
3890909 Samsung 857.28 2015
2787985 Acer 572.20 2013
4900088 LG 232.99 2017
1989000 Nokia 237.24 2006
0089076 Sharp 564.22 2009
2887685 Motorola 569.28 2012
7559090 Pansonic 290.90 2005
2887460 Siemens 457.28 2009
2887685 Apple 969.28 2018
6699001 Lenovo 237.29 2012
9675654 Nokia 388.00 2009
1119002 Motorola 457.28 2008
5000882 Apple 977.27 2016
8888902 Samsung 810.35 2018
5890779 Motorola 457.28 2007
7333403 BenQ 659.00 2009
2999900 Siemens 457.28 2006
6987612 HTC 577.25 2009
8888902 BenQ 410.35 2009
8006832 Motorola 423.22 2008
5555902 SonyEricsson 177.11 2007
9873330 Nokia 677.90 2010
8888902 BenQ 410.35 2009
5909887 Apple 726.99 2017
2389076 BlackBerry 564.22 2010
1119000 SonyEricsson 347.94 2009
【问题讨论】:
标签: java io linked-list nosuchelementexception