Implementation Array Of Java

10:31 PM | , , ,


Hemm... :-) Do you want to know the java program that i made on the first time. I wrote them when I was in the first semester at STMIK Akakom Yogyakarta. They was a simple problem. The problem is save the phone number with an array on java. The program can add the number, edit phone data and remove data from array. 

I am so sorry if I wrote the program with Indonesian language. But I will translate to you one by one. So don't worry about it.

Source code java phonebook

package responsialgo; // this is the package name
import java.util.Scanner;  // importing java.util.Scanner to use the object scanner that can read input from a keyboard
class PhoneBook{ // the class name was PhoneBook


// the block of attribut class have nama (name) , nomor (phone number) , jml (counter how many number stored


public String nama[]=new String [50];
public String nomor[]=new String [50];
public int jml=0; 



//end of block


//this block is method for adding number into array data


public void tambahNomor(){
        Scanner masuk=new Scanner(System.in);
        if(jml<50){ // if  array number is least than 50 then we can add the phone number

        
           //this block read input from keyboard (Name and Phone Number)

            System.out.println("Silahkan masukkan :");
            System.out.print("Nama =");
            nama[jml]=masuk.nextLine();
            System.out.print("Nomor =");
            nomor[jml]=masuk.nextLine();


            //increment the counter

            jml++;


        }else{ //if number of array is not least then 50 then show the message that Phonebook was full

            System.out.println("Phone book sudah penuh!!!");
        }
    }
 //end of block


  //this block is method for show phonebook list . I use looping with For Statement to get array elemen

    public void tampilDaftar(){
        System.out.println("No. Nama  ( Nomor )");
        for(int i=0;i<jml;i++){
            System.out.println( (i+1) + " " + nama[i] + " (" + nomor[i] + ")");
        }
    }
  //end of block


 //this block is method for removing number from array data
    public void hapusDaftar(){
        Scanner masuk=new Scanner(System.in);


 //input index of data which will be removed

        System.out.print("Masukkan indeks phonebook yang akan dihapus = ");
        int indeks=masuk.nextInt()-1;


//searching for data which will deleted with comparing index of data, after that change index of data sliding data before that.

        if(jml>0){
            for(int i=indeks;i<jml;i++){
                nama[i]=nama[i+1];
                nomor[i]=nomor[i+1];
            }
            nama[jml-1]="";
            nomor[jml-1]="";
            jml--;
        }else{
            System.out.println("Tidak ada data yang dihapus!!!");
        }
    }
//end of block


 //this block is method for updating number from array data
    public void gantiNomor(){
          Scanner masuk=new Scanner(System.in); 


//input index of data which will be updated
          System.out.print("Masukkan indeks phonebook yang akan diubah = ");
          int indeks=masuk.nextInt()-1; 


//replace data with a new phone number

          System.out.print("Masukkan nomor baru = ");
          nomor[indeks]=masuk.next();
      }
 } 
//end of block


//The main class to run program
public class DaftarPhoneBook {
         public static void main(String[] args) {
                int pil=0; 
               
               //Create an object HP from class PhoneBook
                PhoneBook HP=new PhoneBook();
                Scanner masuk=new Scanner(System.in);
                System.out.println("PHONEBOOK");
                System.out.println("by Alir Retno");
                System.out.println("====================");
             
                 //Creating a menu to acces program
                System.out.println("Menu :");
                System.out.println("1. Tambah Nomor");  //1. Add Number
                System.out.println("2. Tampilkan Daftar"); //2. Show List
                System.out.println("3. Ganti Nomor"); //3. Update Number
                System.out.println("4. Hapus Nomor"); //4. Delete Number
                System.out.println("5. Keluar"); //5. Exit


               //This is a block try & catch , for handling an exception if user give an error input
                try { 


                      //While user does'nt choose to exit do this looping
                      do {
                            System.out.print("Pilih :");
                            pil=masuk.nextInt(); //input the choice

                            if(pil==1){
                                 HP.tambahNomor();//if user chose 1, please add number

                            }else if(pil==2){
                                 HP.tampilDaftar(); //if user chose 2, please show the list

                           }else if(pil==3){
                                HP.gantiNomor(); //if user chose 3, please update number
                          }else if(pil==4){
                               HP.hapusDaftar(); //if user chose 4, please delete number
                          }
                     }while(pil<5);


                    System.out.println("Anda keluar dari program.");
                    System.out.println("Terima Kasih");



           }catch(Exception err){  //handling if the input is not valid
                   System.out.println("Masukan error!!!");
          }
    }
}





Do you think it's so simple program? . Yeah... If you want to solve problem, familiarize to keep it simple appropriate motto "Keep It Simple and Stupidest" . Do something with a simple and find begin a stupidest method that it's works. After that you can repair the method gradually.


If you want to get my source code please open my Free Download Pages.

Oke guys, I think I  just a little man, so if you have any critics or suggestion for me. Please comment this article.

0 comments:

Post a Comment

Please leave a comment