Solved programming problem

Create an Bank Account and Save data in a File in Java.

Source code

package pkg6;

import java.io.FileReader;

import java.io.IOException;

import java.io.BufferedReader;

import java.io.PrintWriter;

import java.util.Scanner;

public class Main

{

    public static void main(String[] args)

    {

      Scanner sc = new Scanner(System.in);

      BufferedReader br = null;

      String info;

     

      try

      {

        PrintWriter pw = new PrintWriter("output.txt");

        System.out.println("How many account you want to create?");

        int total = sc.nextInt();

        sc.nextLine();

   

       

        for(int i=0;i<total;i++)

        {

            System.out.printf("Name: ");

           

            String name = sc.nextLine();

            System.out.printf("Account No: ");

            int act = sc.nextInt();

            System.out.printf("Balance: ");

            double blnc = sc.nextDouble();

            sc.nextLine();

           

             ///write in file

             pw.println(name);

             pw.println(act);

             pw.println(blnc);          

        }

        pw.close(); ///close in file      

      }

      catch(IOException e)

      {

          System.out.println("Failed to Create File");

      }

         

      ///read

      System.out.println("Saved Data: ");      

      try

      {

         br = new BufferedReader(new FileReader("output.txt"));

         String str;

        ///read from the file

        while((str=br.readLine())!=null)  

        {  

             System.out.println(str);

        }  

      }

        catch(IOException e)

      {

          System.out.println("Failed to Open File");

      }      

    }  

}

Output

How many account you want to create?

2

Name:A

Account No:1

Balance:100.0

Name:B

Account No:2

Balance:200.0

Saved Data:

A

1

100.0

B

2

200.0

 

Join the discussion

Add your comment