Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: How to delete records from a Random-Access File?

  1. #1
    Junior Member
    Join Date
    Mar 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to delete records from a Random-Access File?

    I am currently studying java and i am having trouble with program to simulate a bank account. I Have most of it working but I cannot figure out how to delete records from a Random-Access File. Any help would be appreciated. I have four programs written out - Record, CreateRandomFile, WriteRandomFile and ReadRandomFile.

    // Record.java
    //Record class for the Random Access file program
     
    import java.io.*;
     
    public class Record {
        private int account;
        private String lastName;
        private String firstName;
        private double balance;
        private double overdraftlimit;
     
        //Read a record form the specified RandomAccount
    public void read(RandomAccessFile file)throws IOException
    {
    account = file.readInt();
     
    char first []=new char [15];
    for (int i=0; i<first.length; i++)
    first [i] = file.readChar();
     
    firstName = new String (first);
     
    char last [] = new char[15];
     
    for (int i = 0; i<last.length; i++)
        last [i]=file.readChar();
     
    lastName = new String (last);
    balance = file.readDouble();
    overdraftlimit = file.readDouble();
    }
     
    //Write a record to the specified RandomAccessFile
    public void write (RandomAccessFile file) throws IOException
    {
        StringBuffer buf;
     
        file.writeInt(account);
     
        if (firstName != null)
        buf = new StringBuffer (firstName);
        else
        buf = new StringBuffer(15);
     
        buf.setLength(15);
     
        file.writeChars(buf.toString());
     
        if (lastName !=null)
           buf = new StringBuffer(lastName);
           else
           buf=new StringBuffer (15);
     
           buf.setLength(15);
     
           file.writeChars(buf.toString());
           file.writeDouble(balance);
     
              file.writeDouble(overdraftlimit);
       }
     
       public void setAccount (int a){account = a;}
     
       public int getAccount() {return account;}
     
       public void setFirstName(String f) {firstName =f; }
     
       public String getFirstName () {return firstName;}
     
       public void setLastName (String l) {lastName = l;}
     
       public String getLastName () {return lastName;}
     
       public void setBalance (double b){ balance =b;}
     
       public double getBalance () {return balance;}
     
       public void setOverdraftlimit(double o) {overdraftlimit =o;}
     
       public double getOverdraftlimit () {return overdraftlimit;}
     
       //Note: This method contains a hard coded value
       //for the size of a record of information
     
       public static int size () {return 80;}
     
    }

    // 23.03.09
    // Create Random Access file
     
    import java.io.*;
     
    public class CreateRandomFile {
        private Record blank;
        private RandomAccessFile file;
     
        public CreateRandomFile()
        {
            blank= new Record();
     
            try  {
                file = new RandomAccessFile ("credit.dat", "rw");
     
                for (int i=0; i<100;i++)
                blank.write(file);
            }
            catch(IOException e) {
                System.err.println("File not opened properly\n"+
                                       e.toString() );
                                       System.exit(1);
                                   }
                               }
     
                               public static void main(String args[])
                               {
                                   CreateRandomFile accounts = new CreateRandomFile();
                               }
                           }

    import javax.swing.*;
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class WriteRandomFile extends Frame implements ActionListener
    {
        private JTextField accountField, firstNameField,
                            lastNameField, balanceField, overdraftlimitField;
     
        private JButton enter,
                        done;
     
        private RandomAccessFile output;
        private Record data;
     
        public WriteRandomFile()
        {
            super("Write to random access file");
     
            data = new Record();
     
            try {
                output = new RandomAccessFile("credit.dat","rw");
            }
            catch ( IOException e ) {
                System.err.println(e.toString() );
                System.exit(1);
            }
     
            setSize(350,150);
            setLayout(new GridLayout(6,2));
     
            setLayout(new GridLayout(6,2));
     
            add(new JLabel("Account Number"));
            accountField = new JTextField();
            add(accountField);
     
            add(new JLabel("First Name"));
            firstNameField = new JTextField(20);
            add(firstNameField);
     
            add(new JLabel("Last Name"));
            lastNameField = new JTextField(20);
            add(lastNameField);
     
            add(new JLabel("Balance"));
            balanceField = new JTextField(20);
            add(balanceField);
     
            add(new JLabel("Overdraftlimit"));
            overdraftlimitField = new JTextField(20);
            add(overdraftlimitField);
     
     
            enter = new JButton("Enter");
            enter.addActionListener(this);
            add(enter);
     
            done = new JButton("Done");
            done.addActionListener(this);
            add(done);
     
            setVisible(true);
    }
     
    public void addRecord()
    {
        int accountNumber = 0;
        Double d, db;
     
        if ( ! accountField.getText().equals("")) {
     
            try{
                accountNumber =
                Integer.parseInt(accountField.getText());
     
                if ( accountNumber > 0 && accountNumber <= 1000) {
                    data.setAccount(accountNumber);
                    data.setFirstName(firstNameField.getText());
                    data.setLastName(lastNameField.getText());
                    d = new Double(balanceField.getText());
                    data.setBalance(d.doubleValue());
                    output.seek(
                        (long) (accountNumber-1) * Record.size());
                        data.write(output);
                    db = new Double(overdraftlimitField.getText());
                            data.setOverdraftlimit(db.doubleValue());
                            output.seek(
                            (long) (accountNumber-1) * Record.size());
                            data.write(output);
     
                    }
     
                    accountField.setText("");
                    firstNameField.setText("");
                    lastNameField.setText("");
                    balanceField.setText("");
                    overdraftlimitField.setText("");
                }
                catch (NumberFormatException nfe) {
                    System.err.println(
                        "You must enter an integer account number");
                    }
                    catch ( IOException io){
                        System.err.println(
                            "Error during write to file\n" +
                            io.toString());
                            System.exit(1);
                        }
                    }
                }
     
                public void actionPerformed( ActionEvent e)
                {
                    addRecord();
     
                    if (e.getSource() == done) {
                        try {
                            output.close();
                        }
                        catch ( IOException io ) {
                            System.err.println("File not closed properly\n" +
                            io.toString());
                        }
                        System.exit(0);
                    }
                }
     
                public static void main(String args[])
                {
                    new WriteRandomFile();
                }
            }

    import javax.swing.*;
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.text.DecimalFormat;
     
    public class ReadRandomFile extends Frame implements ActionListener
    {
        private JTextField accountField, firstNameField,
                            lastNameField, balanceField, overdraftlimitField, withdrawField, lodgementField;
     
     
     
        private JButton next,
                        done,
                        withdraw,
                        lodge,
                        delete;
     
        private RandomAccessFile input;
        private Record data;
     
        private RandomAccessFile output;
     
        public ReadRandomFile()
        {
            super( "Read Client File");
     
            try {
                input = new RandomAccessFile("credit.dat","r");
            }
            catch (IOException e ) {
                System.err.println(e.toString() );
                System.exit( 1);
            }
     
            try {
                        output = new RandomAccessFile("credit.dat","rw");
                    }
                    catch ( IOException e ) {
                        System.err.println(e.toString() );
                        System.exit(1);
            }
     
     
     
            data = new Record();
     
            setSize(350, 150);
            setLayout(new GridLayout(10,4));
     
            add( new JLabel("Account Number") );
            accountField = new JTextField();
            accountField.setEditable(false);
            add(accountField);
     
            add(new JLabel("First Name") );
            firstNameField = new JTextField(20);
            firstNameField.setEditable(false);
            add(firstNameField);
     
            add(new JLabel("last Name") );
            lastNameField = new JTextField(20);
            lastNameField.setEditable(false);
            add(lastNameField);
     
            add(new JLabel("Balance") );
            balanceField = new JTextField(20);
            balanceField.setEditable(false);
            add(balanceField);
     
            add(new JLabel("Overdraftlimit"));
            overdraftlimitField = new JTextField(20);
            overdraftlimitField.setEditable(false);
            add(overdraftlimitField);
     
     
            next = new JButton("Next");
            next.addActionListener(this);
            add(next);
     
            done = new JButton("Done");
            done.addActionListener(this);
            add(done);
     
            add(new JLabel("Withdraw"));
            withdrawField = new JTextField(20);
            withdrawField.setEditable(true);
            add(withdrawField);
     
            add(new JLabel("Lodgement"));
            lodgementField = new JTextField(20);
            lodgementField.setEditable(true);
            add(lodgementField);
     
     
            withdraw = new JButton("Withdraw");
            withdraw.addActionListener(
                           new ActionListener() // anonymous inner class
                         {
                             // event handler called when WithdrawalJButton is pressed
                             public void actionPerformed( ActionEvent event )
                           {
                            Withdrawal( event );
                            }
     
                         } // end anonymous inner class
     
                     ); // end call to addActionListener
                     add(withdraw);
     
     
              lodge = new JButton("Lodge");
            lodge.addActionListener(
                           new ActionListener() // anonymous inner class
                         {
                         // event handler called when WithdrawalJButton is pressed
                         public void actionPerformed( ActionEvent event )
                           {
                               Lodgement( event );
                          }
     
                          } // end anonymous inner class
     
                             ); // end call to addActionListener
                     add(lodge);
     
     
            setVisible(true);
     
     
            delete = new JButton("Delete Record");
            delete.addActionListener(
                new ActionListener()
                {
                    public void actionPerformed( ActionEvent event)
                    {
                        DeleteRecord(event);
                    }
     
                }
     
            add(delete);
     
              }
     
     
        public void actionPerformed(ActionEvent e)
        {
            int accountNumber;
     
            if (e.getSource() == next)
            readRecord();
            else
            closeFile();
    }
     
     
        public void readRecord()
        {
            DecimalFormat twoDigits = new DecimalFormat("0.00");
     
            try {
                do {
                    data.read(input);
                } while ( data.getAccount() == 0);
     
                accountField.setText(
                    String.valueOf( data.getAccount()) );
                    firstNameField.setText(data.getFirstName() );
                    lastNameField.setText(data.getLastName() );
                    balanceField.setText(String.valueOf(
                        twoDigits.format(data.getBalance() ) ) );
     
                        overdraftlimitField.setText(String.valueOf(
                        twoDigits.format(data.getOverdraftlimit() ) ) );
     
                    }
                    catch ( EOFException eof) {
                        closeFile();
                    }
                    catch ( IOException e ) {
                        System.err.println("error during read from file\n"+
                                        e.toString() );
                        System.exit(1);
                    }
                }
     
                private void closeFile()
                {
                    try {
                        input.close();
                        System.exit(0);
                    }
                    catch ( IOException e ) {
                        System.err.println("Error closing file\n" +
                                            e.toString() );
                        System.exit(1);
                    }
                }
     
                public static void main(String args[])
                {
                    new ReadRandomFile();
                }
     
                      private void Withdrawal( ActionEvent event )
                      {
                          if (Double.parseDouble(withdrawField.getText()) > Double.parseDouble(balanceField.getText() )  );
     
     
                          balanceField.setText(String.valueOf(Double.parseDouble(balanceField.getText() ) -
                                                                   Double.parseDouble(withdrawField.getText() ) ) );
                                withdrawField .setText("");
     
     
     
     
                                int accountNumber = 0;
                                Double d, db;
     
     
     
                                {
     
                                    try{
                                        accountNumber =
                                        Integer.parseInt(accountField.getText());
     
     
                                        {
                                            data.setAccount(accountNumber);
                                            data.setFirstName(firstNameField.getText());
                                            data.setLastName(lastNameField.getText());
                                            d = new Double(balanceField.getText());
                                            data.setBalance(d.doubleValue());
                                            output.seek(
                                                (long) (accountNumber-1) * Record.size());
                                                data.write(output);
                                            db = new Double(overdraftlimitField.getText());
                                                    data.setOverdraftlimit(db.doubleValue());
                                                    output.seek(
                                                    (long) (accountNumber-1) * Record.size());
                                                    data.write(output);
     
                                            }
     
     
                                        }
     
                                            catch ( IOException io){
     
                                                    System.exit(1);
                                                }
                    }
     
     
     
                       } // end method SubmitJButtonActionPerformed
     
     
                       private void Lodgement( ActionEvent event )
                       {
                           balanceField.setText(String.valueOf(Double.parseDouble(lodgementField.getText() ) +
                           Double.parseDouble(balanceField.getText() ) ) );
                               lodgementField .setText("");
     
     
                                int accountNumber = 0;
                                Double d, db;
     
     
                                {
     
                                    try{
                                        accountNumber =
                                        Integer.parseInt(accountField.getText());
     
     
                                        {
                                            data.setAccount(accountNumber);
                                            data.setFirstName(firstNameField.getText());
                                            data.setLastName(lastNameField.getText());
                                            d = new Double(balanceField.getText());
                                            data.setBalance(d.doubleValue());
                                            output.seek(
                                                (long) (accountNumber-1) * Record.size());
                                                data.write(output);
                                            db = new Double(overdraftlimitField.getText());
                                                    data.setOverdraftlimit(db.doubleValue());
                                                    output.seek(
                                                    (long) (accountNumber-1) * Record.size());
                                                    data.write(output);
     
                                            }
     
     
                                        }
     
                                            catch ( IOException io){
     
                                                    System.exit(1);
                                                }
                    }
     
     
                        }
                        private void DeleteRecord(ActionEvent event)
                        {
     
                                    }
     
    }


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Bank Account Program

    Hello Keith and welcome to the Java programming forums

    Could you please post your 'ReadRandomFile' class again? I can't get it to compile.

    Also, please always put code within the [code] [ /code] tags. (minus the space before /)
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    Junior Member
    Join Date
    Mar 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Bank Account Program

    Sorry i going to post Read Random File again i've made couple of changes it should compile now

     
    import javax.swing.*;
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.text.DecimalFormat;
     
    public class ReadRandomFile extends Frame implements ActionListener {
        private JTextField accountField, firstNameField, lastNameField,
                balanceField, overdraftlimitField, withdrawField, lodgementField;
     
        private JButton next, done, withdraw, lodge, delete;
     
        private RandomAccessFile input;
        private Record data;
     
        private RandomAccessFile output;
     
        public ReadRandomFile() {
            super("Read Client File");
     
            try {
                input = new RandomAccessFile("credit.dat", "r");
            } catch (IOException e) {
                System.err.println(e.toString());
                System.exit(1);
            }
     
            try {
                output = new RandomAccessFile("credit.dat", "rw");
            } catch (IOException e) {
                System.err.println(e.toString());
                System.exit(1);
            }
     
            data = new Record();
     
            setSize(350, 150);
            setLayout(new GridLayout(10, 4));
     
            add(new JLabel("Account Number"));
            accountField = new JTextField();
            accountField.setEditable(false);
            add(accountField);
     
            add(new JLabel("First Name"));
            firstNameField = new JTextField(20);
            firstNameField.setEditable(false);
            add(firstNameField);
     
            add(new JLabel("last Name"));
            lastNameField = new JTextField(20);
            lastNameField.setEditable(false);
            add(lastNameField);
     
            add(new JLabel("Balance"));
            balanceField = new JTextField(20);
            balanceField.setEditable(false);
            add(balanceField);
     
            add(new JLabel("Overdraftlimit"));
            overdraftlimitField = new JTextField(20);
            overdraftlimitField.setEditable(false);
            add(overdraftlimitField);
     
            next = new JButton("Next");
            next.addActionListener(this);
            add(next);
     
            done = new JButton("Done");
            done.addActionListener(this);
            add(done);
     
            add(new JLabel("Withdraw"));
            withdrawField = new JTextField(20);
            withdrawField.setEditable(true);
            add(withdrawField);
     
            add(new JLabel("Lodgement"));
            lodgementField = new JTextField(20);
            lodgementField.setEditable(true);
            add(lodgementField);
     
            withdraw = new JButton("Withdraw");
            withdraw.addActionListener(new ActionListener() // anonymous inner class
                    {
                        // event handler called when WithdrawalJButton is pressed
                        public void actionPerformed(ActionEvent event) {
                            Withdrawal(event);
                        }
     
                    } // end anonymous inner class
     
                    ); // end call to addActionListener
            add(withdraw);
     
            lodge = new JButton("Lodge");
            lodge.addActionListener(new ActionListener() // anonymous inner class
                    {
                        // event handler called when WithdrawalJButton is pressed
                        public void actionPerformed(ActionEvent event) {
                            Lodgement(event);
                        }
     
                    } // end anonymous inner class
     
                    ); // end call to addActionListener
            add(lodge);
     
            setVisible(true);
     
            delete = new JButton("Delete Record");
            delete.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent event) {
                    DeleteRecord(event);
                }
     
            });
            add(delete);
     
        }
     
        public void actionPerformed(ActionEvent e) {
            int accountNumber;
     
            if (e.getSource() == next)
                readRecord();
            else
                closeFile();
     
        }
     
        public void readRecord() {
            DecimalFormat twoDigits = new DecimalFormat("0.00");
     
            try {
                do {
                    data.read(input);
                } while (data.getAccount() == 0);
     
                accountField.setText(String.valueOf(data.getAccount()));
                firstNameField.setText(data.getFirstName());
                lastNameField.setText(data.getLastName());
                balanceField.setText(String.valueOf(twoDigits.format(data
                        .getBalance())));
     
                overdraftlimitField.setText(String.valueOf(twoDigits.format(data
                        .getOverdraftlimit())));
     
            } catch (EOFException eof) {
                closeFile();
            } catch (IOException e) {
                System.err.println("error during read from file\n" + e.toString());
                System.exit(1);
            }
        }
     
        private void closeFile() {
            try {
                input.close();
                System.exit(0);
            } catch (IOException e) {
                System.err.println("Error closing file\n" + e.toString());
                System.exit(1);
            }
        }
     
        public static void main(String args[]) {
            new ReadRandomFile();
        }
     
        private void Withdrawal(ActionEvent event) {
            double Balance = Double.parseDouble(balanceField.getText());
            double o = 0;
     
            if (Balance > o)
     
                balanceField.setText(String.valueOf(Double.parseDouble(balanceField
                        .getText())
                        - Double.parseDouble(withdrawField.getText())));
            withdrawField.setText("");
     
            if (Balance <= o) {
                JOptionPane.showMessageDialog(null,
                        "You Have Insufficant Funds to Request an Overdraft",
                        "Insufficant Funds", JOptionPane.ERROR_MESSAGE); // display
                // error
                // message
                // if
                // user
                // enters
                // invalid
                // input
                balanceField.setText("0");
            }
     
            int accountNumber = 0;
            Double d, db;
     
            {
     
                try {
                    accountNumber = Integer.parseInt(accountField.getText());
     
                    {
                        data.setAccount(accountNumber);
                        data.setFirstName(firstNameField.getText());
                        data.setLastName(lastNameField.getText());
                        d = new Double(balanceField.getText());
                        data.setBalance(d.doubleValue());
                        output.seek((long) (accountNumber - 1) * Record.size());
                        data.write(output);
                        db = new Double(overdraftlimitField.getText());
                        data.setOverdraftlimit(db.doubleValue());
                        output.seek((long) (accountNumber - 1) * Record.size());
                        data.write(output);
     
                    }
     
                }
     
                catch (IOException io) {
     
                    System.exit(1);
                }
            }
     
        } // end method SubmitJButtonActionPerformed
     
        private void Lodgement(ActionEvent event) {
            balanceField.setText(String.valueOf(Double.parseDouble(lodgementField
                    .getText())
                    + Double.parseDouble(balanceField.getText())));
            lodgementField.setText("");
     
            int accountNumber = 0;
            Double d, db;
     
            {
     
                try {
                    accountNumber = Integer.parseInt(accountField.getText());
     
                    {
                        data.setAccount(accountNumber);
                        data.setFirstName(firstNameField.getText());
                        data.setLastName(lastNameField.getText());
                        d = new Double(balanceField.getText());
                        data.setBalance(d.doubleValue());
                        output.seek((long) (accountNumber - 1) * Record.size());
                        data.write(output);
                        db = new Double(overdraftlimitField.getText());
                        data.setOverdraftlimit(db.doubleValue());
                        output.seek((long) (accountNumber - 1) * Record.size());
                        data.write(output);
     
                    }
     
                }
     
                catch (IOException io) {
     
                    System.exit(1);
                }
            }
     
        }
     
        private void DeleteRecord(ActionEvent event) {
     
        }
     
    }

  4. #4
    Junior Member
    Join Date
    Mar 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Bank Account Program

    Sorry forgot to put [code] [ /code] tags on

  5. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Bank Account Program

    Quote Originally Posted by keith View Post
    Sorry forgot to put [code] [ /code] tags on
    No problem. I've added the code tags for you again. They keep everything tidy and makes large amounts of code easier to read.

    You can easily edit your posts by clicking the EDIT button at the bottom right of your posts at any time.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  6. #6
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Bank Account Program

    Hello Keith,

    When I run 'WriteRandomFile' and create an account, nothing is stored in the credit.dat file. Did you know this?

    Is this where all the account details should be stored?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  7. #7
    Junior Member
    Join Date
    Mar 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Bank Account Program

    Yeh, you need to compile the programs in this order first -
    Record, CreateRandomFile, WriteRandomFile, ReadRandomFile
    Than you run WriteRandomFile enter some details , click done
    and run ReadRandomFile to view them

  8. #8
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Bank Account Program

    Quote Originally Posted by keith View Post
    Yeh, you need to compile the programs in this order first -
    Record, CreateRandomFile, WriteRandomFile, ReadRandomFile
    Than you run WriteRandomFile enter some details , click done
    and run ReadRandomFile to view them
    OK yes I see.

    I inserted a record. Data is saved in credit.dat but when I view it in Notepad you can see the account details but its also full of rubbish.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  9. #9
    Junior Member
    Join Date
    Mar 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Bank Account Program

    you can see the details when you open the ReadRandomFile program, run it and click the button next to scroll thru the records you have created

  10. #10
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Bank Account Program

    Quote Originally Posted by keith View Post
    you can see the details when you open the ReadRandomFile program, run it and click the button next to scroll thru the records you have created
    Ah OK I understand how it works now.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  11. #11
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: Bank Account Program

    To delete a record, you will need to read all of the data into your program, remove the record you want to delete, and then re-write it to the file. deleting file content first.

    So i'd say read it all into an array list, line by line if thats how you are storing records, then you can just remove the element from the ArrayList and then write it back into the file.

    Does that make sense?

    EDIT: I decided I'd give you an Example
    package javaapplication14;
     
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.util.ArrayList;
     
    public class Main {
     
        public static void main(String[] args) {
            BufferedWriter bw = null;
            BufferedReader br = null;
     
            try{
                br = new BufferedReader(
                    new FileReader("ex.dat")
                    );
            }catch(Exception e){ System.err.println(e.getMessage()); }
     
            String temp;
            ArrayList<String> data = new ArrayList<String>();
            try{
                while((temp = br.readLine()) != null) data.add(temp);
                br.close();
            }catch(Exception e){ System.err.println(e.getMessage()); }
     
            data.remove(2);
     
            try{
                bw = new BufferedWriter(
                   new FileWriter("ex.dat")
                );
     
                for(int i = 0; i < data.size(); i++){
                    bw.write(data.get(i));
                    bw.newLine();
                }
                bw.close();
            }catch(Exception e){ System.err.println(e.getMessage()); }
        }
    }
    and ex.dat could be something like this to demonstarte
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9

    Chris
    Last edited by Freaky Chris; March 31st, 2009 at 12:37 PM.

Similar Threads

  1. Bank account GUI using swing
    By AlanM595 in forum AWT / Java Swing
    Replies: 5
    Last Post: April 2nd, 2009, 04:39 AM

Tags for this Thread