Minggu, 12 November 2017

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("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){
String input, a, b;
int pilih;
do{
System.out.println("\n\n\n\nKamus Sederhana JAVA");
System.out.println("1. english-indonesian");
System.out.println("2. indonesian-english");
System.out.println("0. exit");
System.out.println();
System.out.println("pilih menu : ");
Scanner plh = new Scanner(System.in);
pilih = plh.nextInt();
switch (pilih){
        case 1:
        System.out.print("masukan 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("masukan kata dalam bahasa indonesia = ");
        Scanner st2= new Scanner(System.in);
        input=st2.nextLine();
       
        kamus kms2=new kamus(input);
       
        kms2.ReadFile();
       
        b=kms2.cari(1);
        if (b.equals(input+" tidak ada dalam kamus"))
        System.out.println(""+kms2.cari(1));
        else
        System.out.println(""+input+" artinya "+kms2.cari(1));
        break;
        }}
        while(pilih!=0);
        }}

Tidak ada komentar:

Posting Komentar