Program Pencarian Data Mahasiswa

Program Pencarian Data Mahasiswa

Salah satu kegunaan dari Metode Searching kita bisa menggunakan untuk pencarian data mahasiswa yang terdiri dari NPM, Nama, Alamat dan pencariannya berdasarkan NPM. Berikut listing program programnya.

<br />
import java.util.Scanner;</p>
<p>public class DataMahasiswa{<br />
	 public static void main(String[ ] args){<br />
		 Scanner in= new Scanner(System.in);<br />
		 //Menentukan Jumlah Data yang akan d Tampung<br />
		 System.out.print(&quot;Jumlah data : &quot;);int N=in.nextInt();<br />
		 //Array untuk menampung Data<br />
		 String npm[ ]=new String[N];<br />
		 String nama[ ]=new String[N];<br />
		 String alamat[ ]=new String[N];</p>
<p>		 //Proses Memasukkan Data<br />
		  for(int i=1;i&lt;=N;i++){<br />
			   System.out.println(&quot;Mahasiswa ke-&quot;+i);<br />
			   System.out.print(&quot;tNPM : &quot;);npm[i-1]=in.next();<br />
			   System.out.print(&quot;tNama : &quot;);nama[i-1]=in.next();<br />
			   System.out.print(&quot;tAlamat : &quot;);alamat[i-1]=in.next();<br />
			   System.out.println();<br />
		  }<br />
		  //Proses Menampilkan Semua Data<br />
		 System.out.println(&quot;nSemua Data Mahasiswa&quot;);<br />
		 System.out.println(&quot;+----+--------------+--------+-----------+&quot;);<br />
		 System.out.println(&quot;| No | NPM | NAMA | ALAMAT |&quot;);<br />
		 for(int i=1;i&lt;=N;i++){<br />
			 System.out.println(&quot;+----+--------------+--------+-----------+&quot;);<br />
			 System.out.println(&quot;| &quot;+i+&quot; | &quot;+npm[i-1]+&quot; | &quot;<br />
			 +nama[i-1]+&quot; &quot;+alamat[i-1]+&quot; |&quot;);<br />
		 }<br />
		System.out.println(&quot;+----+--------------+--------+-----------+n&quot;);<br />
		//Proses Pencarian Data<br />
		int ketemu=0;<br />
		int index[]=new int[N];<br />
		System.out.println(&quot;+ Cari Data Mahasiswa+&quot;);<br />
		System.out.print(&quot;Masukkan NPM : &quot;); String cari=in.next();<br />
		for(int i=0;i<br />
			 if(npm[i].equals(cari)){<br />
				   index[ketemu]=i;<br />
				   ketemu++;<br />
			 }<br />
		}<br />
		//Proses Menampilkan Data yang di Cari<br />
		if(ketemu &gt; 0){<br />
			 System.out.println(&quot;ndata di temukan...&quot;);<br />
			 System.out.println(&quot;+---+--------------+--------+-----------+&quot;);<br />
			 System.out.println(&quot;|No | NPM | NAMA | ALAMAT |&quot;);<br />
			 System.out.println(&quot;+---+--------------+--------+-----------+&quot;);<br />
			 for(int a=1; a&lt;=ketemu; a++){<br />
				  int s=index[a-1];<br />
				  System.out.println(&quot;| &quot;+(index[a-1]+1)+&quot; |<br />
				  &quot;+npm[s]+&quot; | &quot;+nama[s]+&quot; | &quot;+alamat[s]+&quot; |&quot;);<br />
			  }System.out.println(&quot;+---+--------------+--------+-----------+n&quot;);<br />
		 }<br />
		 else{<br />
			   System.out.println(&quot;NPM &quot;+cari+&quot; yang anda cari tidak ada&quot;);<br />
		}<br />
	}<br />
}<br />

Share