I'm using the code below to get the first letter of the first and last name.
It's working, but I see the following problem for a longer name:
Example:
Carlos Eduardo Martins Rego Dutra
public static void main(String[] args) {
String nome = "Carlos Eduardo Martins Dutra do Rego";
String primeiraLetraNomeSobrenome = "";
for (char letra : nome.toCharArray()) {
if (Character.isUpperCase(letra)) {
primeiraLetraNomeSobrenome += letra;
}
}
System.out.println("resultado: "+ primeiraLetraNomeSobrenome);
}
I get: CEMDR
I wanted to get the first and the last one getting CR . Would you have any tips for me to solve this?