public class SearchingApp {
public static void main(String[] args){
int aa = Integer.parseInt(JOptionPane.showInputDialog("Masukkan Jumlah Data!"));
Searching data = new Searching(aa);
for(int i=0;i<aa;i++){
String bb = JOptionPane.showInputDialog("Masukkan Kata ke-"+(i+1));
data.isi(bb);
}
data.tampil();
String cari = JOptionPane.showInputDialog("Masukkan kata yang dicari!");
int f = data.SeqSearch(cari);
if(f<0){
JOptionPane.showMessageDialog(null,"Maaf, Data tidak ditemukan");
}else{
JOptionPane.showMessageDialog(null,"Data ditemukan pada array ke-"+f);
}
}
}
public class Searching {
private String[] a;
private int nElems;
public Searching(int max)
{
a = new String[max];
nElems = 0;
}
public void isi(String value)
{
a[nElems] = value;
nElems++;
}
public void tampil()
{
for(int j=0; j<nElems; j++){
System.out.print(a[j]+" ");
}
System.out.println("\n");
}
public void insertionSort()
{
int in, out;
for(out=1; out<nElems; out++)
{
String temp = a[out];
in = out;
while(in>0 && a[in-1].compareTo(temp)>0)
{
a[in] = a[in-1];
--in;
}
a[in] = temp;
}
}
public int SeqSearch(String key){
for (int i=0; i<a.length-1; i++)
if (a[i].equals(key))
return i;
return -1;
}
}
Tidak ada komentar:
Posting Komentar