How to find two characters in a String

0

I have x number of students with their names and notes, all this I'm going through in a For, but of these students I need to show the number of names that start with A and end in S (I can not include arrangements, please go to the most basic)

This is the code I have for the:

cantEst=Integer.parseInt(JOptionPane.showInputDialog("Ingrese la cantidad de estudiantes : "));

    for (int i=0;i<=cantEst;i++)
    {
        nombre=JOptionPane.showInputDialog("Ingrese nombre del estudiante ");
        nota=Integer.parseInt(JOptionPane.showInputDialog("Ingrese la nota del estudiante "+(nombre))); 
    }
    
asked by Alejandro Monsalve 13.03.2018 в 04:05
source

4 answers

1

You can search to use a variable as a flag in which you will store the matches of the ones that are thrown by the methods startsWith and endsWith respectively.

if(cadena.startsWith("A") && cadena.endsWith("S"))
{
   Contador++;
}

And remember that both functions are case-sensitive so you may want to use a method like toLowerCase beforehand to temporarily convert your string to lowercase or toUpperCase for uppercase.

    
answered by 13.03.2018 / 04:46
source
0

A quick way to know if a String starts and / or ends with a specific letter is using startsWith and endsWith .

Example:

String palabra = "Alonsos"; // Palabra de ejemplo

        // Primera letra
        boolean empiezaConAmin = palabra.substring(0).startsWith("a");
        boolean empiezaConAmay = palabra.substring(0).startsWith("A");

        // Última letra
        boolean terminaConAmin = palabra.substring(0).endsWith("s");
        boolean terminaConAmay = palabra.substring(0).endsWith("S");


        if (empiezaConAmin) {
            System.out.println("Empieza con a");
        }

        if (empiezaConAmay) {
            System.out.println("Empieza con A");
        }

        if (terminaConAmin) {
            System.out.println("Termina con s");
        }

        if (terminaConAmay) {
            System.out.println("Termina con S");
        }

Result:

  

Start with A

     

End with s

If you need to check it in many words, you can easily use this method with a for . Then, within this for use a countable variable that is saved the number of times a word meets those checks.

I hope I have helped you.

    
answered by 13.03.2018 в 04:41
0

Assuming you have an arrangement with the name of the students:

public int cantEstudiantesEmpATermS(String[] estudiantes){
    int cont = 0;
    for(String nombre: estudiantes){
        char inicio = nombre.charAt(0);
        char ultimo = nombre.charAt(nombre.length()-1)
        if((inicio == 'A' || inicio == 'a') && (ultimo == 'S' || ultimo == 's') ){
            cont++;
        }
    }
    return cont;
}

Although you can also use the methods startsWith ("") and endsWith ("")

I hope you find it useful.

    
answered by 13.03.2018 в 16:34
0

With startsWith you can see if a string (String) starts with a given string and endsWith you can see if a string (String) ends with a given string. p>

The following example counts how many names that start with A and end in S:

/**
 *
 * @author SoftMolina
 */
public class Nombres {

    public static void main(String[] args) {

        String nombres[] = {"ANA", "CARLOS", "ANEL", "JUAN", "ANDRES",
            "ANGEL", "JESUS", "MARIANA", "RODRIGO"};

        int contadorA = 0;
        int contadorS = 0;

        for (String nombre : nombres) {

            if (nombre.startsWith("A")) {
                contadorA++;
            }
            if (nombre.endsWith("S")) {
                contadorS++;
            }
        }

        System.out.println("Cuantos nombres empiezan con A: " + contadorA);
        System.out.println("Cuantos nombres terminan con S: " + contadorS);
    }

}

Result:

run:
Cuantos nombres empiezan con A: 4
Cuantos nombres terminan con S: 3
BUILD SUCCESSFUL (total time: 2 seconds)
    
answered by 13.03.2018 в 18:08