Java Tell a character other than a word

4

I know how to count the characters of a word but I do not know how to make it not to repeat itself if that character was already counted

The output that I intend to draw would be a "RATA" entry, an exit from R 1, A 2, T 1.

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        String cadena="";
        char [] Arraycadena ;
        char caracter;
        int contador =0;
        System.out.println("Introduce una palabra");
        cadena=Leer.dato();
        Arraycadena=cadena.toCharArray();
        for(int i =0;i<Arraycadena.length;i++){
            caracter = Arraycadena[i];
            for(int j=0;j<Arraycadena.length;j++){
                if(Arraycadena[j]==caracter){
                    contador++;
                }//Fin Si
            }//Fin Para
            System.out.println(Arraycadena[i]+" "+contador+" veces.");
            contador= 0;
        }//Fin Para

    }

}
    
asked by user7407723 01.03.2018 в 13:56
source

3 answers

5

If you want to count how many times each letter appears you can see if it already existed and, if it existed, add it to an array where you save how many times it appears. So you would have an array of Booleans to see if it already exists and another Integers to see how many times it appears.

Following your code, what you are looking for is something like this:

public static void main(String[] args) {
    String cadena="";
    char [] Arraycadena ;
    char caracter;
    System.out.println("Introduce una palabra");
    cadena=Leer.dato();
    Arraycadena=cadena.toCharArray();

    boolean[] yaEstaElCaracter = new boolean[Character.MAX_VALUE];
    int[] cuantasVeces = new int[Character.MAX_VALUE];

    for(int i =0;i<Arraycadena.length;i++){
        caracter = Arraycadena[i];            
        if(Arraycadena[i]==caracter){
            cuantasVeces[caracter]++;
        }
        yaEstaElCaracter[caracter] = true;
    }//Fin Para


    for(int i = 0; i < yaEstaElCaracter.length; i++){
        if(yaEstaElCaracter[i])
            System.out.println((char) i +" "+cuantasVeces[i]+" veces.");
    }

}

EDITION:

To show them in order I think a less elegant solution, is that you move the array forward and go eliminating the letters that have already appeared and inserting a character that should never appear (for example the blank space and thus can also serve to count characters of a sentence).

It would stay like this:

public static void main(String[] args) {
    String cadena="";
    char [] Arraycadena ;
    char caracter;
    System.out.println("Introduce una palabra");
    cadena=Leer.dato();
    Arraycadena=cadena.toCharArray();

    char[] caracteres = new char[cadena.length()];
    int[] cuantasVeces = new int[cadena.length()];

    for(int i =0;i<Arraycadena.length;i++){
        caracter = Arraycadena[i];   
        caracteres[i] = caracter;
        for(int j = i; j < Arraycadena.length; j++)   {                  
            if(Arraycadena[j]==caracter){
                cuantasVeces[i]++;
                Arraycadena[j] = ' ';
            }
        }    
        if(caracteres[i] != ' ')
            System.out.println(caracteres[i] +" "+cuantasVeces[i]+" veces.");
    }

}

Example of output:

Entrada: 
    RATA
Salida:
    R 1 veces.
    A 2 veces.
    T 1 veces.

Example with a phrase:

Entrada:
    TARTA DE RATA
Salida: 
    T 3 veces.
    A 4 veces.
    R 2 veces.
    D 1 veces.
    E 1 veces.
    
answered by 01.03.2018 / 14:26
source
3

To complement, an implementation based on Map:

String str = "una cadena con muchas letras repetidas";

Map<Character, Integer> map = new LinkedHashMap<Character, Integer>();
for(char c : str.toCharArray()){
    if(map.containsKey(c)) {
        map.put(c, map.get(c) + 1);
    } else {
        map.put(c, 1);
    }
}
System.out.println(map);
//{u=2, n=3, a=6,  =5, c=3, d=2, e=4, o=1, m=1, h=1, s=3, l=1, t=2, r=2, p=1, i=1}
    
answered by 01.03.2018 в 15:34
3

This is an example that I have implemented in a program to do what you are looking for:

public class MiClase {

      public static void main(String args[]) {
          int res = ContarCharUnicos("aaeiou");
          System.out.println(res);
      }

      // Función para contar los caracteres únicos en una cadena
      public static int ContarCharUnicos(String input) {
          boolean[] comprobarChar = new boolean[Character.MAX_VALUE];
          for (int i = 0; i < input.length(); i++) {
              comprobarChar[input.charAt(i)] = true;
          }

          int cuenta = 0;
          for (int i = 0; i < comprobarChar.length; i++) {
              if (comprobarChar[i] == true){
                  cuenta++;
              }
          }

          return cuenta;
      }
}
    
answered by 01.03.2018 в 14:10