Array String reads sentence and then counts vowels and consonants of each word

0

I'm stuck, they ask me for a program:

This stores a phrase, this phrase is saved in a string array. And then you should take each word separately and count each vowel and consonant, if the word has more vowels the program will print that word and give notice.

Example of output:

Entramos Maria nos mira.

The program will return, your first word Maria has more vowels than consonants.

I have done the first part but I am not able to put together the array part with counting the vowels of a String with the charAt method

import java.util.Scanner;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author cbermejo
 */

public class Prueba3 {
    private String palabra1="";
    private String palabra2="";
    private String palabra3="";
    private String palabra4="";
    private String []texto = null;
    Scanner scan = new Scanner (System.in);
    public static void main (String []args){

    Prueba3 programa = new Prueba3 ();
    programa.inici();
}

private void inici(){
    mida();
    arrayIntro();
    Imprimir();

}
private void mida() {

    System.out.println("Dime la medida:");
    int mida = scan.nextInt();
    texto = new String[mida];

}

private void arrayIntro(){


    for ( int i=0; i<texto.length; i++){

        texto [i]= scan.next();

    }

}
private void Imprimir(){
    int contador=0;
    int contador2 =0;
    for ( int i=0; i<texto.length;i++){
        //if(texto[i]==texto[0]){


        if ((texto[i].charAt(i)=='a')|| (texto[i].charAt(i)=='e')||(texto[i].charAt(i)=='i')||(texto[i].charAt(i)=='o')||(texto[i].charAt(i)=='u')){
            contador++;
        }else{
            contador2++;
        }


    }

     System.out.println(contador);
    System.out.println(contador2);   
    }

}
    
asked by Carlos 20.02.2017 в 11:02
source

1 answer

0

A possible solution to your serious problem, having the list of words of the phrase in texto[] , traversing said array and for each palabra traverse its characters and go comparing if it is vowel or consonants. Obviously for each palabra you should see if there are more vowels or more consonants (or equal, if necessary) and once compared reset the counters of vowels and consonants ( contadorVocales and contadorConsonantes )

Here I leave my solution, I hope you serve:

import java.util.Scanner;

public class Prueba3 {
    private String[] texto = null;
    Scanner          scan  = new Scanner(System.in);

    public static void main(String[] args) {

        Prueba3 programa = new Prueba3 ();
        programa.inici();
    }

    private void inici() {
        mida();
        arrayIntro();
        Imprimir();

    }

    private void mida() {

        System.out.println("Dime la medida:");
        int mida = scan.nextInt();
        texto = new String[mida];

    }

    private void arrayIntro() {


        for (int i = 0; i < texto.length; i++) {

            texto[i] = scan.next();

        }

    }

    private void Imprimir() {
        int contadorVocales = 0;
        int contadorConsonantes = 0;
        for (int i = 0; i < texto.length; i++) {
            String palabra = texto[i];

            for (int j = 0; j < palabra.length(); j++) {
                if ((palabra.charAt(j) == 'a') || (palabra.charAt(j) == 'e') || (palabra.charAt(j) == 'i')
                    || (palabra.charAt(j) == 'o') || (palabra.charAt(j) == 'u') || (palabra.charAt(j) == 'A')
                    || (palabra.charAt(j) == 'E') || (palabra.charAt(j) == 'i') || (palabra.charAt(j) == 'O')
                    || (palabra.charAt(j) == 'U')) {
                    contadorVocales++;
                } else {
                    contadorConsonantes++;
                }
            }

            if(contadorVocales > contadorConsonantes){
                System.out.println("La palabra " + palabra + " contiene mas vocales que consonantes");
            } else if(contadorConsonantes > contadorVocales){
                System.out.println("La palabra " + palabra + " contiene mas consonantes que vocales");
            } else {
                System.out.println("La palabra " + palabra + " contiene las mismas vocales que consonantes");
            }

            contadorVocales = 0;
            contadorConsonantes = 0;
        }
    }

}

This code could be improved even by entering the entire phrase and then storing it in the array using the trim (): texto = frase.trim() method. But I preferred to respect your code as much as possible. If you want, tell me.

    
answered by 20.02.2017 / 11:28
source