Counting Java vowels

2

I'm doing a program with which you have to enter a sentence ending in point and you have to say for example the vowel "a" comes out in position 7 and so with all the vowels. The first part that is a finished sentence in point I have done well. But then the part where you have to find the vowels does not work well.

    String texto;
    int c;

    System.out.println("Introduce un texto terminado en punto:");
    texto="";
    do{
        texto=texto+ent.nextLine().trim();
        if(!texto.endsWith(".")) texto+="\n";
    }while(texto.charAt(texto.length()-1)!='.');
    String cadena="aeiou";
        for (int i = 0; i < texto.length(); i++){
            int b = 0;
            b++;
            String a;
            a=cadena.charAt(b);
          System.out.format("\n%d caracter %c",i,text.charAt(a));  
        }

This is the error that shows me:

  

error: incompatible types: char can not be converted to String   a = string.charAt (b); error: incompatible types: String can not be   converted to int System.out.format ("\ n% d character   % c ", i, text.charAt (a));

But if I put char in addition to String peta equally

    
asked by Pepe 04.01.2019 в 15:19
source

3 answers

2

The error you have in these lines:

String a;
a=cadena.charAt(b);

Here is where you are trying to assign a char to a String .

If a has to be a String then you can do:

String a;
a = "" + cadena.charAt(b);

And if it has to be a char then:

char a;
a=cadena.charAt(b);

Which one you choose depends on how you are going to%% of%

On the other hand:

a is incorrect whether a is text.charAt(a) or% char . In String , charAt(x) is a number that indicates the position of the character within x .

If String were a a (in the options above) you could write:

System.out.format("\n%d caracter %c",i, a );
  

But with System.out.format ("\ n% d character% c", i, a); it would not work   Well, the important thing is to filter to see the vowels. If he   charAt (), is not it correct which method to filter I use?

The program, as far as it is done, if the errors are corrected, cycles through each letter of the word or phrase, but does not do any type of inspection or filtering.

What you have left to do is verify, in each cycle, if the letter is a vowel, and if so, tell it.

Then, when leaving the cycle (when all the letters have already been inserted), print the amount for each vowel.

As a help, you will need 5 counters that start at zero, one for each vowel. You can implement it as much as 5 separate variables or in a vector where position 0 is a and position 4 is u.

@gbianchi found other errors in the for cycle related to variable char being reinitialized in each cycle. Plus you're using a single b to do something that actually requires two for nested.

To be able to cycle through all the vowels for each letter of the text you need a for like this:

// Este ciclo recorre el texto ingresado
for (int i = 0; i < texto.length(); i++){
        // El caracter del texto en la posición actual
        char caracterTexto = texto.charAt(i);
        //Este for recorre cada vocal para cada letra del texto ingresado
        for(int b=0 ; b<cadena.length(); b++){
            char a;
            a=cadena.charAt(b);
            System.out.format("\n%d caracter %c",i,a); 
            // Agregar aca la verificación del caracterTexto vs cada vocal (a)
            // Y contar en el contador correspondiente
        }            
}
// En este punto ya están contadas todas las vocales del texto
// Acá se imprime el resultado acumulado en cada uno de los contadores
    
answered by 04.01.2019 / 15:38
source
1

This algorithm allows to determine the position of the vowels within a text as long as it meets the condition that ends in a point.

public void posicionVocales(String texto){
    if(!texto.endsWith(".")) {
      System.out.println("El texto no cumple los requisitos. Falta el \".\"");
    }else{ // Se hacen las operaciones normales de localizar las vocales y colocar su posicion
        char arregloLetras[] = texto.toLowerCase().toCharArray();
        int posicion = 0;
                             // a e i o u
        int vecesRepetidas[] = {0,0,0,0,0};

        for(char letra : arregloLetras){
           switch(letra){
               case 97: // Vocal a
                   System.out.println("Vocal: "+letra+" Posicion-> "+posicion);
                   vecesRepetidas[0] += 1;
                   break;
               case 101: // Vocal e
                   System.out.println("Vocal: "+letra+" Posicion-> "+posicion);
                   vecesRepetidas[1] += 1;
                   break;    
               case 105: // Vocal i
                   System.out.println("Vocal: "+letra+" Posicion-> "+posicion);
                   vecesRepetidas[2] += 1;
                   break;    
               case 111: // Vocal o
                   System.out.println("Vocal: "+letra+" Posicion-> "+posicion);
                   vecesRepetidas[3] += 1;
                   break;    
               case 117: // Vocal u
                   System.out.println("Vocal: "+letra+" Posicion-> "+posicion);
                   vecesRepetidas[4] += 1;
                   break; 
               default :
                   System.out.println("Rechazado: "+letra);
                   break;

           }

           posicion++;
        }

        System.out.println("----------NRO DE APARICIONES------------\n"+
                "a = "+vecesRepetidas[0]+"\n"+
                "e = "+vecesRepetidas[1]+"\n"+
                "i = "+vecesRepetidas[2]+"\n"+
                "o = "+vecesRepetidas[3]+"\n"+
                "u = "+vecesRepetidas[4]       
                        );
    }

}

Then the method is executed within an instance

pm.posicionVocales("Esta es una historia de vocales.");

And the results for the example would be the following:

Vocal: e Position- > 0
Rejected: s
Rejected: t
Vocal: a Position- > 3
Rejected:
Vocal: e Position- > 5
Rejected: s
Rejected:
Vocal: u Position- > 8
Rejected: n
Vocal: a Position- > 10
Rejected:
Rejected: h
Vocal: i Position- > 13
Rejected: s
Rejected: t
Vocal: o Position- > 16
Rejected: r
Vocal: i Position- > 18
Vocal: a Position- > 19
Rejected:
Rejected: d
Vocal: e Position- > 22
Rejected:
Rejected: v
Vocal: o Position- > 25
Rejected: c
Vocal: a Position- > 27
Rejected: l
Vocal: e Position- > 29
Rejected: s
Rejected:.
---------- NRO OF APPEARANCES ------------
a = 4
e = 4
i = 2
o = 2
u = 1

    
answered by 04.01.2019 в 16:30
0

I think this is the easiest of all.

char[] var ="asdfasffae".toCharArray();
int pos = 0;

        for(char x: var) {
            switch(x) {
            case 'a':
            case 'e':
            case 'i':
            case 'o':
            case 'u':
                System.out.println( x + " en la posicion: " + pos);
            }
            pos++;
        }

and produces the following result

a en la posicion: 0
a en la posicion: 4
a en la posicion: 8
e en la posicion: 9

Here is the explanation: inside the for you take a character of the chain and you enter inside the switch which verifies if it is a vowel in the case. If so, print the character value and position.

    
answered by 04.01.2019 в 16:45