Ken Mullins wrote:If I have a name like John Wilkes Booth how would I get the last initial of each name ?
Mohamed Sanaulla | My Blog | Author of Java 9 Cookbook | Java 11 Cookbook
I am trying to get the last initial of each name and then display it. John Wilkes Booth would be nshswaraj gupta wrote:Use the split() method and then through suitable method of String class retrieve the last initial of every string(word)... By the way, what are you trying?
Ken Mullins wrote:I am trying to get the last initial of each name and then display it. John Wilkes Booth would be nsh
I have done tha and have the 3 separate names. How do I obtain the last initial if the length of the names is going to be random ?mohamed sanaullah wrote:
Ken Mullins wrote:I am trying to get the last initial of each name and then display it. John Wilkes Booth would be nsh
Then as Swaraj said- you can split the name using a suitable separator (generally it would be " "). The return would be an array of Strings which would in the case of the Name would be different words constituting the name. Then you can loop through the array and use suitable operations to get the last character of each word and append them.
Ken Mullins wrote:I have done tha and have the 3 separate names. How do I obtain the last initial if the length of the names is going to be random ?
Ken Mullins wrote:
I have done tha and have the 3 separate names. How do I obtain the last initial if the length of the names is going to be random ?mohamed sanaullah wrote:
Ken Mullins wrote:I am trying to get the last initial of each name and then display it. John Wilkes Booth would be nsh
Then as Swaraj said- you can split the name using a suitable separator (generally it would be " "). The return would be an array of Strings which would in the case of the Name would be different words constituting the name. Then you can loop through the array and use suitable operations to get the last character of each word and append them.
I have the names separated. I have extracted the first initial. Do I use the length function -1 to find the last initial in each of the name s ? I think this is where I am headed unless there is simpler way.Mike Simmons wrote:Um, the length of the names is irrelevant, since Ken seems to what the first letter (the initial) of the last name. This separates into two remaining problems:
1. Find the last name.
2. Find the first letter in that last name.
For the first one, ask: how many names are there? What is the index of the last name?
And for the second, there's a simple method in the String class that will do this for you.
After you've solved this problem for "John Smith" and "John A Smith", you might want to think about how to solve it for "John Smith, Jr", "John Smith, PhD" or "John Smith III". Not to mention "Juan de Santo" or "John Davis-Smith". But most of those are probably going to be too complex to consider here. Focus on solving the main problem first.
Mike Simmons wrote:Oh wait - that really is what you want? I messed that earlier. Please stop using the word "initial" to describe what you want, as it is incorrect and misleading. Just say "last letter".
Yes, the length() - 1 approach suggested by two other posters is what you want here.