#WW

#WW
Home » » Program Inputan Kata Java

Program Inputan Kata Java

Written By Unknown on Friday, 21 March 2014 | 01:51

Program Inputan Kata Java

public class Driver {
    public static void main(String[] args) throws IOException {
        Kata k = new Kata();
        BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
        String z;
        int x=0;
        do{
            System.out.println("============================");
            System.out.println("1. Tulis Kata");
            System.out.println("2. Lihat Kata");
            System.out.println("3. Balik Kata");
            System.out.println("4. Exit");
            System.out.println("============================");
            System.out.print("Masukan Pilihan: ");
            z = b.readLine();
            x= Integer.parseInt(z);
            switch(x){
                case 1: System.out.print("Masukan Kata: ");
                        k.setKata(b.readLine());
                        k.simpanObject(k);
                        break;
                case 2: k.bacaObject(k, x);break;
                case 3: k.bacaObject(k, x);break;
                case 4: System.exit(1);
            }
        }while(x!=4);
    }
}





public class Kata implements Serializable{
    private String kata;

    public String getKata() {
        return kata;
    }
    public void setKata(String kata) {
        this.kata = kata;
    }
    public void simpanObject(Kata k) throws IOException{
         FileOutputStream fout = null;
         ObjectOutputStream oout = null;
        try{
            fout = new FileOutputStream("Kelas.txt");
            oout = new ObjectOutputStream(fout);
            oout.writeObject(k);
            oout.flush();
        }
        catch(FileNotFoundException e){
            System.out.println("Tidak ada file");
        }
        catch(Exception e){
            System.out.println("Terjadi exception");
        }
        finally{
            if(fout!=null){
                fout.close();
            }
            else{
                System.out.println("File tidak ditemukan");
            }
        }
    }
    public void bacaObject(Kata k, int i) throws IOException{
         FileInputStream fin = null;
         ObjectInputStream oin = null;
        try{
            fin = new FileInputStream("Kelas.txt");
            oin = new ObjectInputStream(fin);
            while((k=(Kata)oin.readObject())!=null)
            if(i==2){
                k.tampil();
            }
            else{
                k.tampilBalik();
            }
        }
        catch(FileNotFoundException e){
            System.out.println("Tidak ada file");
        }
        catch(NullPointerException e){
            System.out.println("Aw");
        }
        catch(Exception e){
            System.out.print("");
        }
        finally{
            if(fin!=null){
                fin.close();
            }
            else{
                System.out.println("File tidak ditemukan");
            }
        }
    }
    public void tampil(){
        System.out.println("Isi File: "+this.kata);
    }
    public void tampilBalik(){
        String b = "";
        for(int i=this.getKata().length()-1; i>=0; i--){
            b += this.getKata().charAt(i);
        }
        System.out.println("Balik Kata: "+b);
    }
   
   
}

0 komentar:

Post a Comment