The truth is not so bad, you just need to replace the rest of characters with points.
In Java 8 you can use Collections.nCopies with String.join of the form String.join("", Collections.nCopies(n, str))
where Collections.nCopies(n, str)
creates a List object with the object str repeated n times.
and to meet what you need str must be the point and n the length of String -1.
public static void main(String[] args) throws Exception {
List<String> lista = new LinkedList();
lista.add("carlos");
lista.add("c");
lista.add("");
lista.add(null);
for(String data : lista){
System.out.println(myString1(data));
}
}
/**
* si String viene vacio se devuelve un punto
* de lo contrario coloca la primera letra en Mayuscula y el resto con puntos
* @param str
* @return
*/
public static String myString1(String str) {
String dot = ".";
if (str == null || str.isEmpty()) {
return dot;
} else {
return Character.toUpperCase(str.charAt(0)) + String.join("", Collections.nCopies(str.length()-1, dot));
}
}
If you execute it, it will come out:
C.....
C
.
.
If for some reason you use Java 11 , the replay part is easier with repeat :
str.repeat(n);
Of these forms you avoid that a strange character is entered and even be replaced by a point