Kamis, 09 Februari 2012
0 komentar

Menghitung Jumlah Karakter (Java)



Berikut adalah contoh program untuk menghitung karakter dalam java.




package cobahitungchar;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
/**
*
* @author SixOver Collection on http://facebook.com/sixover
*/
public class CobaHitungChar extends JFrame{
JLabel LInput = new JLabel("Masukkan karakter :");
final JTextArea JAreaInput = new JTextArea();
JButton BtnProses = new JButton("PROSES");
JButton BtnReset = new JButton("RESET");
JButton BtnClose = new JButton("CLOSE");

public CobaHitungChar(){
setTitle("Hitung Karakter -S.O.C-");
setSize(600,150);
setLayout(null);
add(LInput);
add(JAreaInput);
add(BtnProses);
add(BtnReset);
add(BtnClose);
LInput.setBounds(10, 10, 150,30);
JAreaInput.setBounds(130, 10, 400, 30);
BtnProses.setBounds(10, 50, 100, 30);
BtnReset.setBounds(120, 50, 100, 30);
BtnClose.setBounds(230, 50, 100, 30);
setVisible(true);
ClickListener cl = new ClickListener();
BtnProses.addActionListener(cl);
BtnReset.addActionListener(cl);
BtnClose.addActionListener(cl);
}
public static void main(String[] args) {
CobaHitungChar cb = new CobaHitungChar();
cb.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// TODO code application logic here
}
private class ClickListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent ae) {
if(ae.getSource()==BtnProses){
//proses hitung karakter
String Inputan = JAreaInput.getText();
String Hasil="";
char[] a= new char[Inputan.length()];
int[] jumlah = new int[Inputan.length()];
for(int z=0;z a[z]=Inputan.charAt(z) ;
}
for(int i=0;i jumlah[i]= hitung(a,a[i]);
}
for (int k=0;k0){
if(a[k]==a[n]){
jumlah[n]=0;
}
n++;
l--;
}
if(jumlah[k]!=0) Hasil+=""+a[k]+"=>"+jumlah[k]+"\n";
}
JOptionPane.showMessageDialog(null, Hasil);
}
if(ae.getSource()==BtnReset){
JAreaInput.setText(null);
}
if(ae.getSource()==BtnClose){
System.exit(0);
}
}
private int hitung(char[] a, char b) {
int z=0;
for(int i=0;i if(a[i]==b)z++;
}
return z;
}

}
}

Untuk lebih lengkapnya download source codenya disini
 
Toggle Footer
Top