How to know how many times you repeat letters?

0

I have the following code, but I want it to show for example: when writing a text, such as House Show, letter C is repeated 1 time, letter A is repeated twice, letter S is repeated 1 time etc. How could I do it?

 public static void main(String[] args) {
Scanner lector = new Scanner (System.in);
String cadena = "";
char [] Arraycadena;
char caracter;
int contador=0;

System.out.println ("Ingrese un texto: ");
cadena = lector.nextLine();
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++;

        }
    }

    System.out.println(Arraycadena[i]+ ""+ contador);
    contador = 0;

}

}

}

    
asked by Swan Andres 08.10.2018 в 16:35
source

2 answers

0

Using a HashMap, in this way the values are not repeated. Your code would look like this:

Scanner lector = new Scanner (System.in);
String cadena = "";
System.out.println ("Ingrese un texto: ");
cadena = lector.nextLine();
Map<Character, Integer> numChars = new HashMap<Character, Integer>();

for (int i = 0; i < cadena.length(); ++i)
{
    char charAt = cadena.charAt(i);

    //Se busca si la letra en el HashMap, como no existe la agregamos con un '1'
    if (!numChars.containsKey(charAt))
    {
        numChars.put(charAt, 1);
    }
    else
    {
        //Como la letra ya existe vamos por el contador de esa letra y 
        //le sumamos uno y la agregamos 'remplazando' la letra anterior
        numChars.put(charAt, numChars.get(charAt) + 1);
    }
}

//Aquí imprimo el HashMap con los resultados 
System.out.println(numChars);

//o aquí la forma de iterar el HashMap
Iterator it = numChars.entrySet().iterator();
while (it.hasNext()) {
    Map.Entry pair = (Map.Entry)it.next();
    System.out.println(pair.getKey() + " = " + pair.getValue());
    it.remove();
}

What we do is that the HashMap is first searched, if the letter does not exist it is added with a 1, if the letter exists the counter of this letter is obtained and 1 is added, what is done is' replace 'the letter by the same letter, but with its increased value, since because it is a HashMap it does not add another letter as it happens in an ArrayList.

    
answered by 09.10.2018 / 16:53
source
0

Your problem is how you have structured the solution.

What you do is traverse the chain in the first loop. In each loop return you go through the entire chain, adding one each time you find the letter of the current character. At the end of each loop of the outer loop you write the current letter with the times you have counted it. This produces the effect you see. For example, you count twice how many 'a' there are, once when you find the first 'a' of 'home', and again when you find the second 'a'.

You have to restructure your solution in the following way:

Initially define a dictionary type variable. This is a complex variable that has keys and values. For each key you can specify a value, and the keys are unique. That is, you can only have one value for a certain key. Let's say it's called contadorLetras .

You must use a single loop that traverses the chain. In each loop loop, what you do is determine the letter and then use it to access the meter counter, adding one to the value associated with the key of the current letter. Something like this (in pseudocode):

if (contadorLetras[caracter] es nulo) {
  contadorLetras[caracter] = 1;
}
else {
  contadorLetras[caracter]++; // o contadorLetras[caracter] = contadorLetras[caracter] + 1;
}

That is, when you find the 'c' of 'house', it will make the counterList ['c'] be 1. When finding the first 'a' it will do the same with the 'a', setting the value to 1. With the 's' equal. But when finding the second 'a' what it will do is increase the value of the 'a' in meterLetras.

At the end of the meterLetras loop, it will be valid:

contadorLetras[c] = 1
contadorLetras[a] = 2
contadorLetras[s] = 1

It only remains for you to go through the meterLetters to write the letter (the key) and how many times it has been found (the value).

Since I do not have a Java VM on hand and it's been a long time since I programmed Java, I'll leave the implementation to you. You will have to find the right type for the variable meterLetras, in the comments suggest a Hashmap .

    
answered by 08.10.2018 в 18:29