listing program kamus java

import java.lang.*;
import java.io.*;
import java.util.*;

public class Kamus {
    private String kata;
    private String english[]=new String[100];
    private String indonesian[]=new String[100];
    private int counter;
 
    public Kamus() {
kata=null;
counter=0;
    }
 
    public Kamus(String _kata){
        kata=_kata;
    }
 
    public void ReadFile (){
        FileReader fReader=null;
        StreamTokenizer stInput = null;
        int i=0;
        String output=null;
     
        try {
            fReader = new FileReader("E:/kamus/Kamus.txt");
         
            stInput =new StreamTokenizer(fReader);
            stInput.nextToken();
         
while(stInput.ttype!=StreamTokenizer.TT_EOF){
if(stInput.ttype ==StreamTokenizer.TT_WORD)
english[i] = stInput.sval;
else
System.out.println("Bad file format");
if(stInput.nextToken()==StreamTokenizer.TT_WORD)
indonesian[i]=stInput.sval;
else
System.out.println("Bad file format");
         
stInput.nextToken();
i++;
counter= counter + 1;
}
        }
       
catch(FileNotFoundException ex){
System.out.println("File not Found : Kamus.txt ");
        }
catch(IOException ex){
            System.out.println(ex.getMessage());
        }
        finally{
try{
if(fReader!=null) fReader.close();
}
catch(IOException ex){
System.out.println(ex);
}
        }
}

    public String Cari (int a){
        int i;
        String output=kata+" tidak ada dalam kamus";
        for (i=0;i<counter;i++){
            if (a==0){
                if (kata.equalsIgnoreCase(english[i])==true)
                output=indonesian[i];
            } else if (a==1){
                if (kata.equalsIgnoreCase(indonesian[i])== true)
                   output=english[i];
                }
        }
        return output;
    }
 
    public static void main(String[] args){
        Scanner phl = new Scanner( System.in);
        String input,a,b;
        int pilih;
        do{
System.out.println("\n\n\nKAMUS SEDERHANA JAVA");
System.out.println("1. English-Indonesian");
System.out.println("2. Indonesiam-English");
System.out.println("0. Keluar");
System.out.println();
System.out.print("Pilihan menu : ");
pilih = phl.nextInt();
     
switch(pilih){
case 1:
System.out.print("Masukkan kata dalam bahasa inggris = ");
Scanner st1=new Scanner(System.in);
input=st1.nextLine();
Kamus kms1 = new Kamus(input);
kms1.ReadFile();
         
a=kms1.Cari(0);
if (a.equals(input+" tidak ada dalam kamus"))
System.out.println(""+kms1.Cari(0));
else
System.out.println(""+input+"+ artinya " +kms1.Cari(0));
break;
   
case 2:
System.out.print("Masukkan kata dalam bahasa indonesia = ");
Scanner st2=new Scanner(System.in);
input=st2.nextLine();
Kamus kms2 = new Kamus(input);
kms2.ReadFile();
a=kms2.Cari(1);
if (a.equals(input+" tidak ada dalam kamus"))
System.out.println(""+kms2.Cari(1));
else
System.out.println(""+input+" Basaha inggris nya " +kms2.Cari(1));
break;
}
        } while(pilih!=0);
    }
}

Comments

Popular Posts