My goal is to create a program that counts hits by entering this keyboard, the problem is that I can not account for those that are beyond the first space, here I leave what I wrote:
public class CuentaCaracteres {
static void toma(String cadena){
System.out.print(cadena);
}
static String lectura(){
String palabra;
Scanner leer=new Scanner(System.in);
return palabra=leer.next();
}
static char lecturaC(){
char buscar;
Scanner leer=new Scanner(System.in);
return buscar=leer.next().charAt(0);
}
public static void main(String[] args) {
String palabra="";
char buscar='b';
int coincidencia=0;
System.out.println("Dime una palabra, te contabilizare el numero de coincidencias");
palabra=lectura();
char[]caracteres= palabra.replaceAll("\W ","" ).toCharArray();
System.out.println("elemento a buscar");
buscar=lecturaC();
for(int i=0; i<caracteres.length;i++){
if(caracteres[i]==buscar) coincidencia++;
System.out.println(caracteres[i]);
}
System.out.println("Hay " + coincidencia + " coincidencias en el vector");
}
}