0
Good afternoon.
I am programming in java and want to get the initials of a name, in case:
"Jonas Barros Santos"
Must appear : JS
The code I use does this, but if the person puts the name with space at the end, my code does not work, giving an Exception.
"Jonas Barros Santos "
I wanted some help to solve this problem.
public static void main(String[] args) {
// TODO code application logic here
String nome = "Jonas Barros Santos ";
String nomeAbrev = null;
int posicaoUltimoEspaco = nome.lastIndexOf(" ");
String primeiraLetraUltimoNome = "";
if (posicaoUltimoEspaco > 0) {
primeiraLetraUltimoNome = nome.substring(posicaoUltimoEspaco + 1, posicaoUltimoEspaco + 2);
}
String primeiraLetra = nome.substring(0, 1);
nomeAbrev = primeiraLetra + primeiraLetraUltimoNome;
System.out.println(nomeAbrev);
}
worked as I needed. Thank you
– AmandaRJ