Example Java Program Pencari Jumlah Kata dalam sebuah kalimat…

Sebuah program untuk mengecheck jumlah kata tertentu dalam sebuah kalimat..


import java.io.*;

class word{

 private StringBuffer kalimat= new StringBuffer();

 private String cari;

 word(){

 System.out.print("Anda belum memasukan String..");

 }

 word(StringBuffer kalimat,String cari){

 this.kalimat.append(kalimat);

 this.cari=cari;

 }

 int find(){

 int i,jml=0;

 int k=0;

 kalimat.append(' ');

 for(i=0;i<this.kalimat.length();i++){

 if(this.kalimat.charAt(i)==' '){

 if(this.cari.equalsIgnoreCase(this.kalimat.substring(k,i))==true){

 jml+=1;

 }

 k=i+1;

 }

 }

 return jml;

 }

}

class carikata{

 public static void main(String args[])throws IOException{

 String kata;

 StringBuffer kalimat=new StringBuffer();

 word car;

 System.out.print("masukan string : " );

 InputStreamReader kt =new InputStreamReader(System.in);

 BufferedReader br = new BufferedReader(kt);

 kalimat.append(br.readLine());

 System.out.print("\n\nmasukan string yang di cari : ");

 kata=br.readLine();

 car=new word(kalimat,kata);

 System.out.print("Hasilnya adalah :"+ car.find()+"\n\n");

 }

 }

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s