2013-06-25 69 views
0

我試圖從文件transactions.txt閱讀,以下是什麼在transactions.txt:NoSuchElementException異常錯誤的Java

Check # 13 : -200.00 
Check # 14 : -100.00 
Withdrawal June 12 : -200.00 
Withdrawal June 17 : -400.00 
Withdrawal June 23 : -100.00 
Deposit : 4000.00 
Deposit : 100.00 
Something else : -1000.00 
Check # 16 : -500.00 
Check # 15 : -100.00 

以下是我的代碼:

import javax.swing.JOptionPane; 
import java.text.DecimalFormat; 
import javax.swing.JFrame; 
import java.awt.Graphics; 
import java.util.StringTokenizer; 
import java.util.Scanner; 
import java.io.File; 
import java.io.FileReader; 
import java.io.FileWriter; 
import java.io.BufferedWriter; 
import java.io.IOException; 
import java.io.FileNotFoundException; 

public class Accounting extends JFrame 
{ 
private BankAccount bankAccount; 

public Accounting() 
{ 
    bankAccount = new BankAccount(getBackground()); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setSize(300, 300); 
    setVisible(true); 
} 

public void balanceCheckBook() 
{ 




    // ***** Write the body of this method ***** 
    // 
    // Using a while loop, read the file transactions.txt 
    // The file transactions.txt contains 
    // transactions between you and your bank 
    // 
    // You will need to call the animate method inside 
    // the body of the loop that reads the file contents 
    // 
    // The animate method takes three arguments: 
    // a String, representing the type of transaction 
    // a double, representing the transaction money amount 
    // a double, representing the new checkbook balance 
    // So if these three variables are: 
    //  transactionName, currentAmount, and balance, 
    // then the call to animate will be: 
    // 
    // animate(transactionName, currentAmount, balance); 
    // 
    // You should make that call in the body of your while 
    // loop, after you have updated the checkbook balance 
    // 

    double balance = 0.00; 
    double currentAmount; 
    String nextLine; 
    StringTokenizer st; 
    String transactionName; 
    Scanner scan = null; 


    scan = new Scanner (new FileReader("transactions.txt")); 

    Scanner callParse; 
    String trans; 
    while ((trans = scan.nextLine()) != null) 
    { 
     scan.useDelimiter(":"); 
     callParse = new Scanner(trans); 
     callParse.useDelimiter(":"); 
    } 


} 

public void animate(String currentTransaction, double currentAmount, double currentBalance) 
{ 
    // set the current transaction in the bankAccount object 
    if (currentTransaction.startsWith("Ch")) 
     bankAccount.setCurrentTransaction(new Check(currentAmount)); 
    else if (currentTransaction.startsWith("With")) 
     bankAccount.setCurrentTransaction(new Withdrawal(currentAmount)); 
    else if (currentTransaction.startsWith("Dep")) 
     bankAccount.setCurrentTransaction(new Deposit(currentAmount)); 
    else 
     bankAccount.setCurrentTransaction(new UnknownTransaction(currentAmount)); 

    // set the currentBalance data member in the bankAccount object 
    bankAccount.updateBalance(currentBalance); 

    repaint(); 
    try 
    { 
    Thread.sleep(3000); // wait for the animation to finish 
    } 
    catch (Exception e) 
    { 
    } 
} 

public void paint(Graphics g) 
{ 
    super.paint(g); 
    bankAccount.draw(g); 
} 

public static void main(String [] args) 
{ 
    Accounting app = new Accounting(); 
    app.balanceCheckBook(); 
} 
} 

我收到錯誤的:

scan = new Scanner (new FileReader("transactions.txt")); 

此外,當我運行的代碼,它告訴我:

Exception in thread "main" java.util.NoSuchElementException: No line found 
    at java.util.Scanner.nextLine(Unknown Source) 
    at Accounting.balanceCheckBook(Accounting.java:73) 
    at Accounting.main(Accounting.java:119) 

我該如何解決這些錯誤?

+0

你確定(transactions.txt)位於正確的位置的文件? – Ketan

+0

@Ketan這會給FileNotFoundException。 –

回答

2

您需要檢查掃描儀是否有使用Scaner.hasNextLine的下一行。

順便說一下,Java文檔是一般奇妙的解釋爲什麼拋出一個特殊的異常。對於Scanner.nextLine,該文檔報告:

拋出:

  • NoSuchElementException - 如果沒有行被發現

經常檢查Java文檔!

+0

謝謝,我會如何實現這一點? – StackP

+0

@StackP調用該方法並查看它返回的內容。 –

+2

@StackP:不要盲目地調用'nextLine',而是調用'hasNextLine'。如果它返回'true',則可以安全地調用'nextLine'。如果返回false,則不是。 – jason

0

它改成這樣:

while (scan.hasNextLine()) 
    { 
     trans = scan.nextLine(); 
     scan.useDelimiter(":"); 
     callParse = new Scanner(trans); 
     callParse.useDelimiter(":"); 

    } 

那麼它應該工作正常

+0

它應該還是會正常工作? –

+0

@SotiriosDelimanolis我沒有在我的本地機器上測試過它,所以我不能肯定地說。問題在於他/他沒有正確檢查是否有下一行。 – sheidaei

+0

@sheidaei當更改爲: ' 掃描儀callParse; String trans; while(scan.hasNextLine) { trans = scan.nextLine(); scan.useDelimiter(「:」); callParse =新掃描儀(反式); callParse.useDelimiter(「:」); }' 它給了我:hasNextLine無法解析或不是字段。 – StackP