Sunday, February 1, 2009

JAVA-1 : Contoh Program Input Data Mahasiswa dan Disimpan dalam suatu File

//Program Input Mahasiswa

import java.io.*;

class mahasiswa{

    public static void main(String[]  args) throws IOException {

        //MEMBUAT FILE
        String namafile="D:/mahasiswa.txt";
        File myfile= new File(namafile);

        if(! myfile.exists())
        {
            System.out.println("Proses buat file...");
            myfile.createNewFile();
        }

        //PILIHAN MENU
        BufferedReader key=new BufferedReader(new InputStreamReader(System.in));
        //INPUT DATA

        String cek="Y";

        int menu =0;
        while(menu !=3)
        {
            System.out.println("Menu Pilihan:");
            System.out.println("-------------------------");
            System.out.println("1. Input data Mahasiswa");
            System.out.println("2. Lihat semua data");
            System.out.println("3. Exit");
            System.out.println("-------------------------");
            System.out.print("Pilihan   :");

            menu=Integer.parseInt(key.readLine());

            if (menu==1){

                FileWriter fw =new FileWriter(myfile);
                BufferedWriter bw=new BufferedWriter(fw);

                cek="y";

                while(cek.equalsIgnoreCase("y")){

                    System.out.print("NIM MAHASISWA        :");
                    bw.write("NIM MAHASISWA :" +key.readLine()+ "  ");

                    System.out.print("NAMA MAHASISWA       :");
                    bw.write("NAMA MAHASISWA :" + key.readLine() + " ");

                    System.out.print("JURUSAN MAHASISWA    :");
                    bw.write("JURUSAN MAHASISWA :" +key.readLine()+"\n");

                    System.out.println("-------------------------");

                    System.out.print("Tambah data Mahasiswa lagi ??? (y/n)  : ");
                    cek=key.readLine();

                }

                bw.close();

            }else if(menu==2){

                //MENAMPILKAN DATA
                FileReader fr =new FileReader(namafile);
                BufferedReader br =new BufferedReader(fr);

                String isi=new String();

                while((isi=br.readLine())!=null)
                {
                    System.out.println(isi);
                }

                System.out.println("\n");

            }

        }

        System.out.println("Proses selesai... \n");
    }
}

// Selamat Mencoba

About This Blog

About This Blog

  © Blogger template Brooklyn by Ourblogtemplates.com 2008

Back to TOP