2011-03-29 39 views
0

我正在研究一個代碼,該代碼從輸入文件讀取有關客戶的數據並將其存儲到客戶對象的鏈接列表中。鏈表實現不是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 
+0

hm ..也許嘗試:「johnny depp」 – smas 2011-03-29 18:47:05

+0

該文件看起來像你想分析什麼?什麼行(來自您的堆棧跟蹤)導致問題?沒有更多的信息,我會冒險猜測你的Integer.parseInt()調用正在傳遞一串字符而不是一個數字。 – rancidfishbreath 2011-03-29 19:00:12

回答

0

的第一條記錄有更多的線(它有一個「固定利息每日」),比第二,所以你可能會認爲你正在閱讀的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) { 

希望這有助於!

+0

我確實考慮它是否具有「固定利率」。查看定義對象Acc1,Acc2和Acc3的一組if條件。在有條件的情況下,它讀取利率並向前推進。 – dawnoflife 2011-03-29 18:51:24

+0

好的,讓我再檢查一次。這次更加小心:-) – andyb 2011-03-29 18:57:21

+0

有什麼好運?我一直在試圖調試它,但它只是向我展示了一個空洞的accID:/ – dawnoflife 2011-03-29 19:47:03