输入/输出Java方法

输入/输出Java方法,java,graphics,exception-handling,io,output,Java,Graphics,Exception Handling,Io,Output,说明如下: 要处理事务,您需要从transactions.txt文件中一次读取一行,并解析您检索的字符串。您可以为此使用Scanner类。分隔符将是冒号。然后处理交易;您不需要检查事务的类型。只需将交易金额添加到支票簿余额中即可。增加负交易金额将按预期减少余额。确保在适当的地方使用try/catch块 处理完每个事务后,调用animate方法。此方法属于Accounting类,因此您将在不使用对象引用的情况下调用animate。如果动画方法如下所示,则API public void animat

说明如下:

要处理事务,您需要从transactions.txt文件中一次读取一行,并解析您检索的字符串。您可以为此使用Scanner类。分隔符将是冒号。然后处理交易;您不需要检查事务的类型。只需将交易金额添加到支票簿余额中即可。增加负交易金额将按预期减少余额。确保在适当的地方使用try/catch块

处理完每个事务后,调用animate方法。此方法属于Accounting类,因此您将在不使用对象引用的情况下调用animate。如果动画方法如下所示,则API

public void animate { String currentTransaction, double currentAmount, double currentBalance, }
如您所见,animate方法采用三个参数:currentTransaction是交易名称(“存款”,例如),currentAmount是交易金额(-45.00),currentBalance是支票簿的当前余额。假设您有一个名为transactionName的字符串变量、一个名为amount的双变量和另一个名为balance的双变量,则对动画的调用如下所示:

animate( transactionName, amount, balance);
调用动画时,窗口将按地理位置显示当前事务。它还将显示交易金额(红色,如果为负,则为蓝色,如果为正)和当前支票簿余额(黑色)。通过将以前的支票簿余额添加到当前金额,您将能够在头脑中计算当前支票簿余额,并确定您的程序是否正常工作

到达文件末尾时,打印最终余额并将其写入名为balance.txt的文件

到目前为止,我在会计学课上写了这么多:

import javax.swing.*;
import java.text.DecimalFormat;
import java.awt.Graphics;
import java.util.*;
import java.io.*;

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;
 }

 public void animate( String currentTransaction, double currentAmount, double currentBalance )
 {
   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 ) );


   bankAccount.updateBalance( currentBalance );

   repaint( );
   try
   {
    Thread.sleep( 3000 );
   }
   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( );
 }
}

使用扫描仪是读取java文件的最简单方法

import java.util.Scanner
然后

然后

根据您的文件结构,您可以使用myScanner.next()和myScanner.nextInt()分别获取令牌和int,令牌通常是由空格分隔的单词

[编辑]

op想要解释如何扫描每一行,这里是一个演示

while (myScanner.hasNextLine()) {
    line = myScanner.nextLine();
    Scanner lineReader = new Scanner(line);
    String firstWord = lineReader.next();
    String secondWord = lineReader.next();
    double thirdWordValue = lineReader.nextDouble();
    double fourthWordValue = lineReader.nextDouble();

    animate(firstWord, thirdWordValue, fourthWordValue);

}    

public void animate{String currentTransaction,double currentAmount,double currentBalance,}:这是不可能的。您的问题是什么?好像你在要求一个solution@Stephan我在问一个解决办法。这不是为了上课,也不是为了作业。这是一本纯粹的教科书上的练习。我无法理解这一点,所以我想也许请别人教我怎么做可以帮助我理解它。谢谢!在我发布问题后,我才明白这一点。但是我该如何调用animate函数呢?将
line
变量解析成一个字符串,并将其两倍传递给animate()@StackP您可以使用另一个扫描仪执行此操作,然后调用lineScanner.next()或lineScanner.nextDouble(),我知道这可能要求太多,但因为我是为了自己的练习而尝试的,你能帮我做这个吗?阅读代码让我明白发生了什么,但在这一点上,我不知道我需要做什么。@StackP伙计,我在工作。。。但是,是的,我添加了一些东西,我不知道你的文本文件是什么样子,所以我编了一些东西,每当你在扫描仪上调用next()方法,它就会得到下一个标记,并返回一个字符串,如果下一个标记是一个数字,调用nextDouble(),它就会返回一个双精度
String line;
while (myScanner.hasNextLine()) {
    line = myScanner.nextLine();
    //i think you'll call your animate function in here

}
while (myScanner.hasNextLine()) {
    line = myScanner.nextLine();
    Scanner lineReader = new Scanner(line);
    String firstWord = lineReader.next();
    String secondWord = lineReader.next();
    double thirdWordValue = lineReader.nextDouble();
    double fourthWordValue = lineReader.nextDouble();

    animate(firstWord, thirdWordValue, fourthWordValue);

}