我正在使用Java中的TreeMap结构。该键包含字符':',值是事物列表。问题是,当我调试程序停止在这条线(不工作了...)扫描仪hasNextLine()不访问Java中的最后一行
if (!string.isEmpty()) {
**string = jin.nextLine();**
}
我真的不知道有什么可以是问题。下面是我的代码。数据(我保留日期变量)和ListOfBills(我保留Bill类的对象列表)是两个其他类。
public void read(InputStream in) throws ParseException {
Scanner jin = new Scanner(in);
TreeMap<Date, ListOfBills> tree = new TreeMap<Date, ListOfBills>();
ListOfBills obBill = new ListOfBills();
Data data;
String string = jin.nextLine();
while (jin.hasNextLine()) {
if (string.contains(":")) {
data = new Data((string));
string = jin.nextLine();
while (!string.contains(":")) {
String[] parts1 = string.split(" ");
obBill.listOfBills.add(new Bill(Integer.parseInt(parts1[0]), Float.parseFloat(parts1[2]),
parts1[3], Float.parseFloat(parts1[5])));
if (!string.isEmpty()) {
string = jin.nextLine();
}
}
tree.put(data.date1, obBill);
}
}
for (Date date : tree.keySet()) {
System.out.println(date + "\n");
}
jin.close();
}
你是不是测试'jin.hasNextLine()'每次调用'string = jin.nextLine();'所以这肯定是容易出错的。 –
一旦'if(string.contains(“:”))'为false,你将进入一个无限循环。 –