Other Answers, such as the one by WJS, give a direct, correct solution to the specifics of the Question: appending . to each capital letter. My Answer here is more for fun.
tl;dr
Using code points rather than char.
System.out.println(
String.join( // Join a bunch of strings into a single `String` object.
"" , // Join the strings without any delimiter between them.
Arrays.stream( // Turn an array into a stream of elements for processing.
"Association of Lions, and Tigers, and Bears, oh my!!"
.split( " " ) // Returns an array of string objects, `String[]`.
)
.filter( ( String part ) -> ! part.isBlank() ) // Skip any string elements without significant text.
.map( ( String part ) -> part.codePoints().boxed().toList() ) // Generate a stream of `int` integer numbers representing the code point numbers of each character in the string element. Via boxing, convert those `int` primitives into `Integer` objects. Collect the `Integer` objects into a `List`.
.filter( ( List < Integer > codePoints ) -> Character.isUpperCase( codePoints.get( 0 ) ) ) // Skip any list of code points if the first code point represents a character other than an uppercase letter.
.map( ( List < Integer > codePoints ) -> Character.toString( codePoints.get( 0 ) ) + "." ) // Extract the first code point `Integer` number, turn into a `String` containing the single character represented by that code point, and append a FULL STOP.
.toList() // Collect all those generated strings (single character plus FULL STOP) to a list.
)
);
A.L.T.B.
Code points
I recommend making a habit of using code point integer numbers rather than the legacy char type. As a 16-bit value, char is physically incapable of representing most characters.
This code makes heavy use of streams. If not yet comfortable with streams, you could replace with conventional loops.
Pull apart the phrase.
System.out.println( "input = " + input );
String[] parts = input.split( " " );
if ( parts.length == 0 ) { throw new IllegalArgumentException( "Input must consist of multiple words separated by spaces." ); }
System.out.println( "parts = " + Arrays.toString( parts ) );
Convert each part of the phrase into code point integer numbers.
List < List < Integer > > codePointsOfEachPart =
Arrays
.stream( parts )
.filter( part -> ! part.isBlank() )
.map( part -> part.codePoints().boxed().toList() )
.toList();
Filter for those parts of the phrase that begin with an uppercase letter. From each of the qualifying parts, extract the first letter, append a FULL STOP, and collect to a list.
List < String > abbreviations =
codePointsOfEachPart
.stream()
.filter( ( List < Integer > codePoints ) -> Character.isUpperCase( codePoints.get( 0 ) ) )
.map( codePoints -> Character.toString( codePoints.get( 0 ) ) + "." )
.toList();
// Join the collection of UppercaseLetter + FULL STOP combos into a single piece of text.
String result = String.join( "" , abbreviations );
return result;
}
When run:
input = Association of Lions, and Tigers, and Bears, oh my!!
parts = [Association, of, , Lions,, and, Tigers,, and, Bears,, oh, my!!]
output = A.L.T.B.
Pull that all together for your copy-paste convenience.
private String acronym ( final String input )
{
// Pull apart the phrase.
System.out.println( "input = " + input );
String[] parts = input.split( " " );
if ( parts.length == 0 ) { throw new IllegalArgumentException( "Input must consist of multiple words separated by spaces." ); }
System.out.println( "parts = " + Arrays.toString( parts ) );
// Convert each part of the phrase into code point integer numbers.
List < List < Integer > > codePointsOfEachPart =
Arrays
.stream( parts )
.filter( part -> ! part.isBlank() )
.map( part -> part.codePoints().boxed().toList() )
.toList();
// Filter for those parts of the phrase that begin with an uppercase letter.
// From each of the qualifying parts, extract the first letter, append a FULL STOP, and collect to a list.
List < String > abbreviations =
codePointsOfEachPart
.stream()
.filter( ( List < Integer > codePoints ) -> Character.isUpperCase( codePoints.get( 0 ) ) )
.map( codePoints -> Character.toString( codePoints.get( 0 ) ) + "." )
.toList();
// Join the collection of UppercaseLetter + FULL STOP combos into a single piece of text.
String result = String.join( "" , abbreviations );
return result;
}
That code could be shortened, into a single line of code! Not that I necessarily recommend doing so.
System.out.println(
String.join(
"" ,
Arrays.stream(
"Association of Lions, and Tigers, and Bears, oh my!!"
.split( " " )
)
.filter( part -> ! part.isBlank() )
.map( part -> part.codePoints().boxed().toList() )
.filter( ( List < Integer > codePoints ) -> Character.isUpperCase( codePoints.get( 0 ) ) )
.map( codePoints -> Character.toString( codePoints.get( 0 ) ) + "." )
.toList()
)
);
A.L.T.B.