将文件数据读入LinkedList错误
我正在研究一个代码,该代码从输入文件读取有关客户的数据并将其存储到客户对象的链接列表中。链表实现不是JVM之一。当使用readFile()读取数据时,它给我一个NumberFormatException: For input string: "Ben Affleck"
错误。这里是方法。逻辑的基本思想是最初读取第一条记录,并将其设置为链接列表的头部,然后读取后续记录。该错误发生在if条件时,它检查重复的帐户ID。我编码的方式是,如果id的匹配,然后跳过那么多行到下一个记录。 Acd()方法在链接列表中按升序输入项目。帮助将不胜感激。如果问题不清楚,请让我知道。将文件数据读入LinkedList错误
public static int readFile(String filename, LinkedList<Customer> review) throws IOException{
Scanner scan = new Scanner (new File (filename));
/*Reading the first record separatly*/
Customer head = new Customer();
Node<Customer> first = new Node<Customer>(head);
String[] a = scan.nextLine().split("=");
int accId = Integer.parseInt(a[1].trim());
a = scan.nextLine().split("=");
String name = a[1].toUpperCase().trim();
a = scan.nextLine().split("=");
String address =a[1].trim();
a = scan.nextLine().split("=");
String phone_number =(a[1].trim());
a = scan.nextLine().split("=");
String date_of_birth =(a[1].trim());
a = scan.nextLine().split("=");
double balance =(Double.parseDouble(a[1].trim()));
a= scan.nextLine().split("=");
String accType =(a[1].trim());
if (accType.equals("Saving")){
Customer temp = new Account1();
Node<Customer> firstItem = new Node<Customer>(temp);
first = firstItem;
}
else if(accType.equals("Checking")){
Customer temp = new Account2();
Node<Customer> firstItem = new Node<Customer>(temp);
first = firstItem;
}
else if(accType.equals("Fixed")){
Customer temp = new Account3();
Node<Customer> firstItem = new Node<Customer>(temp);
first = firstItem;
a = scan.nextLine().split("=");
((Account3)first.item).set_intRate(Double.parseDouble(a[1].trim()));
}
first.item.set_account_id(accId);
first.item.set_name(name);
first.item.set_address(address);
first.item.set_phone_number(phone_number);
first.item.set_date_of_birth(date_of_birth);
first.item.set_balance(balance);
review.head= first;
count = count+1;
scan.nextLine();// resets the buffer reader
while (scan.hasNext()&& count>0){
Customer item = new Customer();
Node<Customer> temp = new Node<Customer>(item);
String[] st = scan.nextLine().split("=");
Customer ctr = new Customer();
Node<Customer> counter = new Node<Customer>(ctr);
counter=review.head; // counter pointing to head
int i=0;
while(counter!=null){
if(Integer.parseInt(st[1].trim())== review.getItem(i).get_accountid()){ // checking for duplicate records
System.out.println("This account id is already in use so the record won't be read");
while(!scan.nextLine().equals(" "))
scan.nextLine();
scan.nextLine(); //to bring the reader back to the accoutnId
}
else
break;
int AccId = Integer.parseInt(st[1].trim());
st = scan.nextLine().split("=");
String AccName = st[1].toUpperCase().trim();
st = scan.nextLine().split("=");
String AccAdd =st[1].trim();
st = scan.nextLine().split("=");
String AccPhNum =(st[1].trim());
st = scan.nextLine().split("=");
String AccDob =(st[1].trim());
st = scan.nextLine().split("=");
double AccBal =(Double.parseDouble(st[1].trim()));
st= scan.nextLine().split("=");
String AccType =(st[1].trim());
if (AccType.equals("Saving")){
Customer a1 = new Account1();
Node<Customer>Item = new Node<Customer>(a1);
temp = Item;
} else if(AccType.equals("Checking")){
Customer a2 = new Account2();
Node<Customer>Item = new Node<Customer>(a2);
temp = Item;
} else if(AccType.equals("Fixed")){
Customer a3 = new Account3();
Node<Customer>Item = new Node<Customer>(a3);
temp = Item;
st = scan.nextLine().split("=");
((Account3)temp.item).set_intRate(Double.parseDouble(a[1].trim()));
}
temp.item.set_account_id(AccId);
temp.item.set_name(AccName);
temp.item.set_address(AccAdd);
temp.item.set_phone_number(AccPhNum);
temp.item.set_date_of_birth(AccDob);
temp.item.set_balance(AccBal);
if (scan.hasNextLine()){
scan.nextLine();
}
review.insertAcd(temp.item);
count= count+1;
counter=counter.next;
}
if (count>=30){
System.out.println("The number of records read has exceeded the limit and it will stop reading now");
break;
}
}
return count;
}
输入文件是:
Account Id = 123
Name = Matt Damon
Address = 465 Ripley Boulevard, Oscar Mansion, Singapore 7666322
DOB = 10-10-1970
Phone Number = 790-3233
Account Balance = 405600.00
Account Type = Fixed
Fixed Daily Interest = 0.05
Account Id = 126
Name = Ben Affleck
Address = 200 Hunting Street, Singapore 784563
DOB = 25-10-1968
Phone Number = 432-4579
Account Balance = 530045.00
Account Type = Saving
Account Id = 65
Name = Salma Hayek
Address = 45 Mexican Boulevard, Hotel California, Singapore 467822
DOB = 06-04-73
Phone Number = 790-0000
Account Balance = 2345.00
Account Type = Checking
Account Id = 78
Name = Phua Chu Kang
Address = 50 PCK Avenue, Singapore 639798
DOB = 11-08-64
Phone Number = 345-6780
Account Balance = 0.00
Account Type = Checking
Account Id = 234
Name = Zoe Tay
Address = 100 Blue Eyed St, Singapore 456872
DOB = 15-02-68
Phone Number = 456-1234
Account Balance = 600.00
Account Type = Saving
Account Id = 2350
Name = Zoe Tay
Address = 100 Blue Eyed St, Singapore 456872
DOB = 15-02-68
Phone Number = 456-1234
Account Balance = 600.00
Account Type = Fixed
Fixed Daily Interest = 0.055
的第一条记录有更多的线(它有一个“固定利息每日”),比第二,所以你可能会认为你正在阅读的String但它实际上是一个Double(反之亦然)。因此,您需要修改代码,以考虑此额外行,或将其从第一条记录中删除,因为代码预计为
int, String, String, String, String, double, String
,而第一条记录为
int, String, String, String, String, double, String, double
。
这不是真正的最佳解决方案,因为您正在重复一段代码。它真的可以在我认为的一个循环中。像我最初所说的那样,这绝对是一个类型转换问题。您试图从不包含数字的字符串中获取整数。 Java正确地告诉你,有no parsable Integer。
我会尝试编译你的代码,看看我能不能确定确切的错误,但是我上面写的应该给你足够的想法来找出破坏的位置。基本上你认为你正在读取你输入文件的一行,而你实际上是在上面或下面的行上。
编辑:嗯,我已经破解了你的代码,并得到它编译。从最初的检查看来,马特达蒙看起来不错,但这是第二个循环,这是不正确的。你有一个看起来像这样的代码:
while (scan.hasNext()&& count>0){
Customer item = new Customer();
Node<Customer> temp = new Node<Customer>(item);
String[] st = scan.nextLine().split("=");
....
while(counter!=null){
if(Integer.parseInt(st[1].trim())== review.getItem(i).get_accountid()){
...
} else {
break;
}
}
}
帐号st[1].trim()
(这是126的方式输入文件)不匹配,因为马特·达蒙是唯一一个至今,所以代码break
小号脱离while
的条件,然后继续阅读下一行 - “本阿弗莱克”。然后它再次进入内部while
循环,并尝试在“Ben Affleck”上做Integer.parseInt
,正如您所看到的是NumberFormatException
。
编辑2:
已经看过了你的其他问题,它看起来像你所得到的SO社区写了很多的应用程序为您服务!很显然你正在学习Java,但在我看来,这可能不是学习Java的最佳方式!不要担心,我们都在那里:-)
没有通过您的确切代码,我不能真正回答这个问题。请注意,由于缺少相关类,main()和import
语句,因此无法单独编译上述表单。
所以我的答案是怎么回事,因为我看不出有任何理由的第一条记录应单独阅读,我认为功能是它所需要做的过于复杂的是大多伪为您的整个READFILE功能。
Scanner scan = new Scanner (new File (filename));
// maintain collecction of Account Number <-> Account details
Map<Integer, Customer> accounts = new HashMap<Integer, Customer>();
String[] aLine = null;
while (scan.hasNext()) {
// read all of one account details
aLine= scan.nextLine().split("=");
int accId = Integer.parseInt(aLine[1].trim());
aLine= scan.nextLine().split("=");
String name = aLine[1].toUpperCase().trim();
etc...
String accType =(a[1].trim());
if (accType.equals("Saving")) {
...
} else {
...
}
// create Integer version of the accId to use as the key (the lookup)
// into the collection of details
Integer key = new Integer(accId);
if (accounts.containsKey(key)) {
// already added to the collection so
// no need to create a new Customer
} else {
// create new Customer
Customer c = new Customer();
c.set_account_id(accId);
etc...
// and add to the collection
c.put(key, c);
}
// skip over blank lines
while(!scan.nextLine().equals(" ")) {
scan.nextLine();
}
}
您可能需要一些约束添加到while条件限制添加帐户数(因为你有你的现有代码)。例如:
while (scan.hasNext() && accounts.size() < 30) {
希望这有助于!
我确实考虑它是否具有“固定利率”。查看定义对象Acc1,Acc2和Acc3的一组if条件。在有条件的情况下,它读取利率并向前推进。 – dawnoflife 2011-03-29 18:51:24
好的,让我再检查一次。这次更加小心:-) – andyb 2011-03-29 18:57:21
有什么好运?我一直在试图调试它,但它只是向我展示了一个空洞的accID:/ – dawnoflife 2011-03-29 19:47:03
hm ..也许尝试:“johnny depp” – smas 2011-03-29 18:47:05
该文件看起来像你想分析什么?什么行(来自您的堆栈跟踪)导致问题?没有更多的信息,我会冒险猜测你的Integer.parseInt()调用正在传递一串字符而不是一个数字。 – rancidfishbreath 2011-03-29 19:00:12