• Post Reply Bookmark Topic Watch Topic
  • New Topic

extracting last initial  RSS feed

 
Ken Mullins
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I have a name like John Wilkes Booth how would I get the last initial of each name ?
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ken Mullins wrote:If I have a name like John Wilkes Booth how would I get the last initial of each name ?


Like - B for John Wilkes Booth, S for Mohamed Sanaulla, J for John?
 
swaraj gupta
Ranch Hand
Posts: 186
C++ Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
swaraj 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?
I am trying to get the last initial of each name and then display it. John Wilkes Booth would be nsh
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 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 Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?


You get the length of each string. Then the last character would be - length-1?
 
swaraj gupta
Ranch Hand
Posts: 186
C++ Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ken, do you have any idea about determining the length of string objects? If you have then use it and do remember one thing indexing always starts from zero.
Ken Mullins wrote:
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 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 ?
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ken Mullins
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
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
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ken Mullins
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

That worked. Thanks for your help. I will work on the other name configurations you suggested for practice.
 
Wojciech Bakun
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe something like that:

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!