Listing Program CariKata.java package com.indah.kmp; import import import import import import import
java.io.BufferedReader; java.io.File; java.io.FileNotFoundException; java.io.FileReader; java.io.IOException; java.util.ArrayList; java.util.List;
import import import import import import import import import import import
android.app.Activity; android.content.Intent; android.os.Bundle; android.os.Environment; android.view.View; android.widget.ArrayAdapter; android.widget.AutoCompleteTextView; android.widget.Button; android.widget.EditText; android.widget.TextView; android.widget.Toast;
public class CariKata extends Activity { private AutoCompleteTextView actv;
EditText edkata, edarti; Button b1 , b2, btn2kembali; TextView tvarti, tvnotfound; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b1 = (Button) findViewById (R.id.btncari); b2 = (Button) findViewById (R.id.button_tambah); btn2kembali= (Button) findViewById (R.id.btn2_cari);
Universitas Sumatera Utara
List<String> listkata = new ArrayList<String>(); File fl = new File(Environment.getExternalStorageDirectory() + "/contoh"); String ck = fl.toString()+"/"+"kerjaku.csv"; BufferedReader br = null; String line = ""; String cvsSplitBy = "#"; try { br = new BufferedReader(new FileReader(ck)); while ((line = br.readLine()) != null) { String[] kamus = line.split(cvsSplitBy); String kata_b = kamus[0]; listkata.add(kata_b);
} } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } }
String[] kata = listkata.toArray(new String[listkata.size()]);
ArrayAdapter adapter = new ArrayAdapter (this,android.R.layout.simple_list_item_1,kata);
Universitas Sumatera Utara
actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1); actv.setAdapter(adapter); b1.setOnClickListener( new View.OnClickListener(){ public void onClick(View arg0) { actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1); tvarti = (TextView) findViewById(R.id.tvArti);
File folder = new File(Environment.getExternalStorageDirectory() "/contoh"); String csv = folder.toString()+"/"+"kerjaku.csv";
+
KMP k = new KMP(); String cari = actv.getText().toString(); System.out.println("Terjemahan kata "+cari); BufferedReader br = null; String line = ""; String cvsSplitBy = "#";
int ketemu = 0; try { br = new BufferedReader(new FileReader(csv)); while ((line = br.readLine()) != null) { String[] kamus = line.split(cvsSplitBy); String kata = kamus[0]; String arti = kamus[1]; int first_occur_position = k.kmp(kata , cari); if(first_occur_position >= 0){ tvarti.setText(kata + "="+arti); System.out.println(kata + "="+arti); ketemu += 1; }
Universitas Sumatera Utara
} } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } System.out.println("Proses Baca Data Selesai"); if(ketemu<1){ tvarti.setText("Kata Tidak Ditemukan"); } edkata.setText(""); } });
b2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent i = new Intent(CariKata.this, TambahKata.class); i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(i); } });
btn2kembali.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub
Universitas Sumatera Utara
Intent i = new Intent(CariKata.this, MainActivity.class); startActivity(i); } }); } }
KMP.java package com.indah.kmp; publicclass KMP { publicint[] prekmp(String pattern) { int[] next = newint[pattern.length()]; int i=0, j=-1; next[0]=-1; while (i<pattern.length()-1) { while (j>=0 && pattern.charAt(i)!=pattern.charAt(j)) j = next[j]; i++; j++; next[i] = j; } return next; } publicint kmp(String text, String pattern) { int[] next = prekmp(pattern); int i=0, j=0; while (i
=0 && text.charAt(i)!=pattern.charAt(j)) j = next[j]; i++; j++; if (j==pattern.length()) return ipattern.length(); } return -1; }
Universitas Sumatera Utara
}
MainActivity.java package com.indah.kmp; import import import import import import import import import import import import import import import
java.io.BufferedReader; java.io.File; java.io.FileNotFoundException; java.io.FileReader; java.io.IOException; android.support.v7.app.ActionBarActivity; android.content.Intent; //indah tmbah android.os.Bundle; android.os.Environment; android.view.Menu; android.view.MenuItem; android.view.View; android.widget.Button; android.widget.EditText; android.widget.TextView;
publicclass MainActivity extends ActionBarActivity {
Button cari, tambah; @Override protectedvoid onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.dashboard); cari = (Button) findViewById (R.id.button_cari); tambah = (Button) findViewById (R.id.button_tambah); cari.setOnClickListener(new View.OnClickListener() { @Override publicvoid onClick(View v) { Intent i = new Intent(MainActivity.this, CariKata.class); startActivity(i); }
Universitas Sumatera Utara
});
tambah.setOnClickListener(new View.OnClickListener() { @Override publicvoid onClick(View v) { // TODO Auto-generated method stub Intent i = new Intent(MainActivity.this, TambahKata.class); startActivity(i); } }); }
@Override publicboolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); returntrue; } @Override publicboolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_settings) { returntrue; } returnsuper.onOptionsItemSelected(item); } }
TambahKata.java package com.indah.kmp; import import import import import import
java.io.BufferedReader; java.io.File; java.io.FileNotFoundException; java.io.FileReader; java.io.FileWriter; java.io.IOException;
Universitas Sumatera Utara
import java.util.ArrayList; import java.util.List; import import import import import import import import
android.app.Activity; android.content.Intent; android.os.Bundle; android.os.Environment; android.view.View; android.widget.Button; android.widget.EditText; android.widget.TextView;
publicclass TambahKata extends Activity { Button btn2; Button b1; Button btnsimpan; TextView tvarti, tvnotfound; EditText edkata, edarti;
@Override protectedvoid onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.tambah_kata); b1 = (Button) findViewById (R.id.btncari); btn2 = (Button)findViewById(R.id.btn2); btn2.setOnClickListener( new View.OnClickListener(){ @Override publicvoid onClick(View v) { Intent i = new Intent(TambahKata.this, MainActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(i); } }); b1.setOnClickListener(new View.OnClickListener() { @Override publicvoid onClick(View v) {
Universitas Sumatera Utara
edkata = (EditText)findViewById(R.id.edkata); edarti = (EditText)findViewById(R.id.edarti); String kata = edkata.getText().toString(); String arti = edarti.getText().toString(); List<String> listkata = new ArrayList<String>(); listarti = new ArrayList<String>();
List<String>
File folder = new File(Environment.getExternalStorageDirectory() "/contoh"); String csvFile = folder.toString()+"/"+"kerjaku.csv";
+
BufferedReader br = null; String line = ""; String cvsSplitBy = "#"; try { br = new BufferedReader(new FileReader(csvFile)); while ((line = br.readLine()) != null) { String[] kamus = line.split(cvsSplitBy); String kata_b = kamus[0]; String arti_b = kamus[1]; listkata.add(kata_b); //terus di masukin ke kotak listarti.add(arti_b); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } }
Universitas Sumatera Utara
} System.out.println("Proses Baca Data Selesai"); try {
String NamaFile = folder.toString()+"/"+"kerjaku.csv"; FileWriter writer = new FileWriter(NamaFile); int p = listkata.size(); for(int i= 0 ; i < p; i++){ String s_kata = listkata.get(i); String s_arti = listarti.get(i); writer.append(s_kata); writer.append('#'); writer.append(s_arti); writer.append('\n');
} writer.append(kata); writer.append('#'); writer.append(arti); writer.append('\n'); writer.flush(); writer.close(); } catch(IOException e) { e.printStackTrace(); } System.out.println("Proses Input Selesai"); } }); } }
Tentang.Java
Universitas Sumatera Utara
package com.indah.kmp; import import import import import
android.app.Activity; android.content.Intent; android.os.Bundle; android.view.View; android.widget.Button;
publicclass Tentang extends Activity { Button btn1;
@Override protectedvoid onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.tentang);
btn1 = (Button)findViewById(R.id.button1); btn1.setOnClickListener( new View.OnClickListener(){ @Override publicvoid onClick(View v) { Intent i = new Intent(Tentang.this, MainActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(i); } }); } } Bantuan.Java package com.indah.kmp; import import import import import
android.app.Activity; android.content.Intent; android.os.Bundle; android.view.View; android.widget.Button;
publicclass Bantuan extends Activity { Button btn1;
@Override
Universitas Sumatera Utara
protectedvoid onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.tentang);
btn1 = (Button)findViewById(R.id.button1); btn1.setOnClickListener( new View.OnClickListener(){ @Override publicvoid onClick(View v) { Intent i = new Intent(Bantuan.this, MainActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(i); } }); } } activity_main.xml
<Button android:id="@+id/button_tambah" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/btncari" android:layout_alignBottom="@+id/btncari" android:layout_alignParentRight="true" android:text="Tambah Kata"/> <Button
Universitas Sumatera Utara
android:id="@+id/btncari" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/textView3" android:layout_alignLeft="@+id/textView1" android:layout_marginBottom="18dp" android:text="Cari" /> <requestFocus />
Universitas Sumatera Utara
android:textSize="27sp" android:textStyle="bold" /> <Button android:id="@+id/btn2_cari" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button_tambah" android:layout_alignParentBottom="true" android:layout_alignRight="@+id/button_tambah" android:layout_marginBottom="24dp" android:layout_marginRight="17dp" android:text="Kembali" />
bantuan.xml <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="textMultiLine" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Kembali" />
Universitas Sumatera Utara
dashboard.xml <Button android:id="@+id/button_cari" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_marginRight="35dp" android:layout_marginTop="86dp" android:layout_toLeftOf="@+id/button_tambah" android:height="80dp" android:text="Cari Kata" android:width="120dp" /> <Button android:id="@+id/button_tambah" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignTop="@+id/button_cari" android:layout_marginRight="26dp" android:height="80dp" android:text="Tambah Kata" android:width="120dp" /> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button_tambah" android:layout_centerVertical="true" android:height="80dp" android:text="Bantuan" android:width="120dp" /> <Button android:id="@+id/button3"
Universitas Sumatera Utara
android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button_cari" android:layout_centerVertical="true" android:height="80dp" android:text="Tentang Aplikasi" android:width="120dp" />
tambah_kata.xml <Button android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_marginBottom="24dp" android:layout_marginRight="27dp" android:text="Kembali" /> <EditText android:id="@+id/edarti" android:layout_width="match_parent" android:layout_height="wrap_content"
Universitas Sumatera Utara
android:layout_alignBottom="@+id/btncari" android:layout_alignLeft="@+id/textView3" android:layout_alignRight="@+id/btn2" android:layout_marginBottom="57dp" android:ems="10"> <requestFocus /> <EditText android:id="@+id/edkata" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView3" android:layout_alignRight="@+id/edarti" android:layout_below="@+id/textView2" android:layout_marginTop="27dp" android:ems="10" />
Universitas Sumatera Utara
<Button android:id="@+id/btncari" android:layout_width="146dp" android:layout_height="wrap_content" android:layout_above="@+id/btn2" android:layout_alignLeft="@+id/textView3" android:layout_marginBottom="76dp" android:text="Simpan" />
Tentang.xml
Universitas Sumatera Utara
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView2" android:layout_marginTop="47dp" android:text="Kembali" />
Bantuan.xml <Button android:id="@+id/button1"
Universitas Sumatera Utara
android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Kembali" />
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android " package="com.indah.kmp" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Universitas Sumatera Utara
Universitas Sumatera Utara