【问题标题】:NoSuchElementException Error JavaNoSuchElementException 错误 Java
【发布时间】:2013-06-25 16:04:38
【问题描述】:

我正在尝试从文件 transactions.txt 中读取,以下是 transactions.txt 中的内容:

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)

我可以做些什么来修复这些错误?

【问题讨论】:

  • 您确定您的文件(transactions.txt)位于正确的位置吗?
  • @Ketan 这会给出 FileNotFoundException。

标签: java parsing error-handling io try-catch


【解决方案1】:

您需要使用Scaner.hasNextLine 检查扫描仪是否有下一行。

顺便说一句,Java 文档通常在解释为什么抛出特定异常方面非常出色。对于Scanner.nextLine,文档报告:

投掷:

  • NoSuchElementException - 如果没有找到行

始终检查 Java 文档!

【讨论】:

  • 谢谢,我该如何实现呢?
  • @StackP 调用该方法,看看它返回什么。
  • @StackP:不要盲目调用nextLine,而是调用hasNextLine。如果它返回true,则调用nextLine 是安全的。如果返回false,则不是。
【解决方案2】:

改成这样:

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

    }

那么它应该可以正常工作

【讨论】:

  • @SotiriosDelimanolis 我没有在我的本地机器上测试过它,所以我不能确定。问题是她/他没有正确检查是否有下一行。
  • @sheidaei 将其更改为:' Scanner callParse;字符串反式; while (scan.hasNextLine) { trans = scan.nextLine(); scan.useDelimiter(":"); callParse = new Scanner(trans); callParse.useDelimiter(":"); }' 它给了我: hasNextLine 无法解析或不是字段。
  • @StackP 我不知道为什么它会给你这个错误。看看这个链接:tutorialspoint.com/java/util/scanner_hasnextline.htm 这是一个关于如何使用 hasNextLine 的好教程。你用的是什么IDE?它是否在运行时给您错误?还是在编译时?
  • @StackP 代码必须是while(scan.hasNextLine()),我原来没有include()。感谢 SotiriosDelimanolis 修复它。
猜你喜欢
  • 2017-03-05
  • 1970-01-01
  • 2022-12-03
  • 1970-01-01
  • 1970-01-01
  • 2021-03-31
  • 2021-08-08
  • 1970-01-01
相关资源
最近更新 更多