3

the program is supposed to create a number of blank records to a .txt file, which can be edited later to add infos like name and balance..etc. my problem is whenever i put another record, the balance from the first account disappears (but the name doesn't). if i try to add another account, the balance from the previous account disappear as well. its probably overwritten or something.

how do i fix this?

import java.io.*;

public class CreateBankFile {
   private RandomAccessFile file;

   public static void main(String[] args) throws IOException {
      CreateBankFile blank = new CreateBankFile();
   }

   public CreateBankFile() throws IOException{
      initialize();
      int task = 0;
      while(task == 0)
      {
         writeRecord();
      }
   }

   private void initialize() {
      Account acc = new Account();

      try {
         File fileName = new File("C:/Documents and Settings/x/Desktop/hehe.txt");
         file = new RandomAccessFile( fileName, "rw" );

         // Create 100000 blank records in the file
         for ( int i = 0; i < 100001; i++)
            acc.write( file );
      } catch ( IOException ioex ){
         System.out.println("File does not exists");
      }
   }

    private void writeRecord() throws IOException {
       Account acc = new Account();
       BufferedReader data = new BufferedReader(new InputStreamReader(System.in));
       System.out.println("Enter Account # ");
       String accNo = data.readLine();
       int recordNumber = Integer.parseInt(accNo);

       try {

          File fileName = new File("C:/Documents and Settings/x/Desktop/hehe.txt");
          file = new RandomAccessFile( fileName, "rw" );

          // Write the record to the file
          if ( recordNumber > 0 && recordNumber < 100001 ) {

             System.out.println("Enter Account Name: ");
             String neym = data.readLine();
             System.out.println("Enter Remaining Balance: ");
             String numbah = data.readLine();
             acc.setName(neym);
             acc.setBalance(numbah);

             // Go to the record location
             file.seek( (recordNumber - 1) * acc.size() );

             // Write the record out
             acc.write( file );

             System.out.println("\nUpdate Succesful!\n");
             System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
          }
       } catch ( Exception ioex ){
          System.out.println("File does not exists");
       }
   }
}


class Account {
   private String name;
   private String balance;

   // Constructor
   public Account() { this ("", ""); }

   // Overloaded Constructor
   public Account(String n, String b) {
      name = n;
      balance = b;
   }

   // Public method to read the data from the file
   public void read( RandomAccessFile file ) throws IOException {
      setName( padData( file ) );
      setBalance ( padData( file ) );
   }

   // Public method to write the data to the file
   public void write( RandomAccessFile file ) throws IOException {
      writeData( file, getName() );
      writeData( file, getBalance() );
   }

   // The method that actually writes the data to the file

   private void writeData(RandomAccessFile f, String n) throws IOException {
      StringBuffer buf = null;

      if ( n != null )
         buf = new StringBuffer( n );
      else
         buf = new StringBuffer( 20 );
         buf.setLength( 20 );
         f.writeChars( buf.toString() );
   }

   // This pads the data so to make the data all the same size
   // we will go for a size of 20
   private String padData( RandomAccessFile f ) throws IOException {
      char data[] = new char[ 20 ], temp;

      for ( int i = 0; i < data.length; i++ ) {
         temp = f.readChar();
         data[ i ] = temp;
      }

      return new String ( data ).replace( '\0', ' ' );
   }

   // This method hard codes the value for the size of the record
   // which is 20
   public static int size() { return 20; }

   // The get and set methods
   public void setName(String n) { name = n; }

   public void setBalance(String b) { balance = b; }

   public String getName() { return name; }

   public String getBalance() { return balance; }
}
4

3 に答える 3

1

他の人が指摘している他の問題は別として。

   buf.setLength( 20 );

あなたは20文字しか書いていません。if ステートメントにも中かっこが必要です。

private void writeData(RandomAccessFile f, String n) throws IOException {
  StringBuffer buf = null;

  if ( n != null )
     buf = new StringBuffer( n );
  else {
     buf = new StringBuffer( 20 );
     buf.setLength( 20 );
  }

  f.writeChars( buf.toString() );
}
于 2013-08-06T00:53:04.110 に答える
0

問題はこれです:

// Create 100000 blank records in the file
for ( int i = 0; i < 100001; i++)
    acc.write( file );

あなたはあなたのinitialize()関数にそれを持っていますCreateBankFile()main()

したがって、プログラムを実行するたびに、ファイルが再初期化されます。一掃。したがって、そのコードを削除すると、プログラムが機能するはずです。

(最初にファイルを初期化する必要がありますが、ファイルを初期化するための2番目のプログラムを作成するか、それをユーザーオプションにするか、ファイルが存在するかどうかを検出して初期化する必要があります。)

于 2013-08-06T00:54:37.073 に答える