2
I am using the code below to get the first letter of the name and surname.
It’s working, but I see the following problem for a longer name:
Example:
Carlos Eduardo Martins Dutra do Rego
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 receive: CEMDR
I wanted to catch the first and the last CR. Would you have any tips for me to fix this?
The algorithm is naive, several things can go wrong, but it is not your problem, what was asked is answered, the first is a little better, to make a robust would be even more complex.
– Maniero