0

I have created a simple program that will create a bank account and balance This program will ask the number of accounts you want to create then you will input account number and balances.

Now the problem is when its writing new account number it just Overwriting it, so the text that entering in my file is only 1 line;

Here's my current code:

    import java.util.*;

import javax.swing.JOptionPane;

public class CreateBankFile {
    public static Formatter sample;
    public static int account;
    public static int balance;
    public static void main(String[] args) {
        createBank();
        createFile();
        addRecords();
        closeFile();
    }

    public static void createBank() {
        int loops = Integer.parseInt(JOptionPane.showInputDialog(null,
                "How many accounts \nyou wanted to create?"));
        for (int i = 1; i <= loops; i++) {
            account = Integer.parseInt(JOptionPane.showInputDialog(null,
                    "Enter account number:"));
            balance = Integer.parseInt(JOptionPane.showInputDialog(null,
                    "Enter balance:"));
        }
    }

    public static void createFile() {
        try {
            sample = new Formatter("BankAccounts.txt");
        } catch (Exception e) {
            System.out.println("You got an error!");
        }
    }

    public static void addRecords() {
        sample.format("%s%s", account + "\t", balance);
    }

    public static void closeFile() {
        sample.close();
        JOptionPane.showMessageDialog(null, "You successfully created bank accounts!");
    }
}

Thank you, hope you will answer this question

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 2
    There are like a zillion tutorials on how to write files in Java... – Smutje Jan 26 '15 at 07:54
  • 1
    You'll need to append the new text, not just write. – Stultuske Jan 26 '15 at 07:54
  • possible duplicate of [how to write bytes in a file without overwriting it](http://stackoverflow.com/questions/4568940/how-to-write-bytes-in-a-file-without-overwriting-it) – Evan Knowles Jan 26 '15 at 07:55
  • possible another duplicate of [How to append text to an existing file in Java](http://stackoverflow.com/questions/1625234/how-to-append-text-to-an-existing-file-in-java) – alizelzele Jan 26 '15 at 07:57

2 Answers2

0
// create an output stream to the file in append mode rather than overwrite mode
FileOutputStream out = new FileOutputStream("BankAccounts.txt", true);
// create a Formatter which writes to this stream
Formatter formatter = new Formatter(out);

The Java tutorials and the javadoc are your friends. Use them.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thank you but it still writing in one line, i want it in another line i tried to "\n" but it still goes on – jAvA tHe Gamer Jan 26 '15 at 08:03
  • Then make sure the lines you append to your file contain a newline character at the end. They don't. – JB Nizet Jan 26 '15 at 08:04
  • sample.format("%s%s%s", account + "\t", balance,"\n"); that's the code that i changed for, or i need to change all my codes to another way? like appending? i dont use append – jAvA tHe Gamer Jan 26 '15 at 08:08
  • I would put the \n in the pattern itself: it's not dynamic at all. And if on Windows, I would use `\r\n`: sample.format("%s\t%s\r\n", account, balance); – JB Nizet Jan 26 '15 at 08:19
0

Your method createBank stores only the last inputs for account and balance in the variables and addRecords is only called once.
Call addRecords inside the loop in createBank and createFile before createBank.

public static void main(String[] args) {
   createFile();
   createBank();
   closeFile();
}

public static void createBank() {
   int loops = Integer.parseInt(JOptionPane.showInputDialog(null,
           "How many accounts \nyou wanted to create?"));
   for (int i = 1; i <= loops; i++) {
       account = Integer.parseInt(JOptionPane.showInputDialog(null,
               "Enter account number:"));
       balance = Integer.parseInt(JOptionPane.showInputDialog(null,
               "Enter balance:"));
       addRecords();
   }
}

And add a newline in addRecords.

Gren
  • 1,850
  • 1
  • 11
  • 16