How To Count Specific Character Occurrence In A String Using Java
In This Java Tutorial We Will See How To Count The Number Of Selected Char Occurrences In A JTextField Text Using Java Programming Language And NetBeans Editor.
Project Source Code:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String txt = jTextField1.getText().toLowerCase();
int occurrence = 0;
for(int i = 0; i < txt.length(); i++)
{
if(txt.charAt(i) == jTextFieldChar.getText().toLowerCase().charAt(0)){
occurrence++;
}
}
jTextFieldResult.setText( Integer.toString(occurrence));
}
///////////////OUTPUT:
Download Projects Source Code