我正在研究一个代码,该代码从输入文件中读取有关客户的数据,并将它们存储到客户对象的链接列表中。链表实现不是JVM实现。在使用readFile()读取数据时,它给出了NumberFormatException: For input string: "Ben Affleck"
错误。这是方法。逻辑的基本思想是最初读取第一条记录并将其设置为链表的头部,然后读取后续记录。在检查重复帐户ID时if条件期间发生错误。我编码的方式是,如果id的匹配,则跳过那么多行到下一条记录。 Acd()方法在链表中按升序输入项目。帮助将不胜感激。如果问题不清楚,请告诉我。
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
答案 0 :(得分:0)
第一条记录有更多行(它具有“固定每日兴趣”)而不是第二条记录,因此您可能认为您正在读取字符串,但它实际上是一个Double(反之亦然)。所以你需要修改你的代码,要么考虑这个额外的行,要么将其从第一个记录中删除,因为你的代码期望 int, String, String, String, String, double, String
,而第一个记录是int, String, String, String, String, double, String, double
。
这不是解决此问题的最佳解决方案,因为您正在重复一段代码。我认为它真的可以在一个循环中。这绝对是我最初说过的类型转换问题。您试图从不包含数字的String中获取整数。 Java正确地告诉您有no parsable Integer。
我会尝试编译你的代码,看看我是否能确定确切的错误,但我上面写的内容应该给你足够的想法来找出破损的位置。基本上你认为你正在阅读输入文件的一行,而你实际上是在上面或下面的行。
编辑:我已经破解了你的代码并将其编译好了。从最初的检查来看,Matt Damon看起来没问题,但这是第二个不正确的循环。您的代码如下所示:
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)不匹配,因为到目前为止Matt Damon是唯一一个,所以代码break
不在内部{ {1}}条件然后继续阅读下一行 - “本阿弗莱克”。然后它再次进入内部while
循环并尝试在“Ben Affleck”上执行while
,如您所见Integer.parseInt
。
编辑2:
看了你的其他问题,看起来你正在让SO社区为你编写很多应用程序!很明显你正在学习Java,但在我看来,这可能不是学习Java的最好方法!不过不要担心,我们都去过那里: - )
如果没有单步执行您的确切代码,我无法完全回答这个问题。请注意,它不能单独编译上面给出的表单,因为它缺少依赖类,main()和NumberFormatException
语句。
所以我的答案将主要是整个 readFile 函数的伪代码,因为我认为没有理由为什么第一条记录应该单独读取,我觉得这个函数对于它需要的东西来说过于复杂要做。
import
您可能希望在while条件中添加一些约束以限制添加的帐户数(就像您在现有代码中一样)。例如:
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();
}
}
希望这有帮助!