2016-02-10 53 views
0

我有我所有的代碼工作正常,這個程序應該讀取一個文件,然後打印出來,並計算出BMI,預算成本並顯示它。我只是試圖顯示所有prems的總價格問題。無法返回正確的值,NumberFormatException

+0

這行都是這樣的'在Project.premPrice(項目.java:48)'? – nullpointer

+0

totalPrem + = premPrice(customerDetails);這可能是我試圖計算錯誤總數的方法 – Nick

回答

-2

您有問題在代碼中的這3行。

Integer.parseInt(customerDetails[4]); 
int height = Integer.parseInt(customerDetails[6]); 
int weight = Integer.parseInt(customerDetails[7]); 

問題可能與文件內容有關。找出最好的方法是打印customerDetails[4],customerDetails[6]customerDetails[7]。該值應該只包含數字。如果有其他字符,你會在問題中得到提及的錯誤。

+0

問題不在於文件內容,查看輸出,錯誤在displayMessage2中,no在displayMessage中 –

+0

是在執行Integer的displayMessage2中調用premPrice。 parseInt函數。 –

+0

誰編輯我的答案,變成垃圾和誰投下的票,關心解釋,以便我們可以糾正它。 –

1

看到輸出,我知道問題不是文件的內容。

我覺得你的問題是在這裏:

// Read information from file and store in array  
    String[] customerDetails = new String[9]; 
    Scanner inputFile = new Scanner(file); 
    for (int i = 0; i < TOTAL_ACC; i++){ 
     for(int j = 0 ; inputFile.hasNext() && j < 9 ; j++){ 
      customerDetails[j] = inputFile.nextLine(); 
     } 
     displayMessage(customerDetails); 
     customerDetails = new String[9]; //Look this line 
    } 
     displayMessage2(customerDetails); //Look this line 
     inputFile.close(); 
    } 

你叫displayMessage2(customerDetails);customerDetails = new String[9];

而且displayMessage2電話premPrice(customerDetails)premPrice後做int age = Integer.parseInt(customerDetails[4]);

+0

我認爲是對的,我只是試着移動displayMessage2(customerDetails);在customerDetails = new String [9]之前;它的工作,但它打印出displayMessage2所有6次,我只需要它在文件的末尾打印一次,總不正確。 – Nick

+0

@Nick你想做什麼?您計算「所有保費總價格:$」的邏輯是什麼? –

+0

我試圖從所有計算出來的客戶端收取保費,並將它們合併爲一個總計。 https://gyazo.com/f99b2a2cb48d8e0b378dc2e54293adfc – Nick