aspose file tools
The moose likes Beginning Java and the fly likes extracting last initial Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "extracting last initial" Watch "extracting last initial" New topic
Author

extracting last initial

Ken Mullins
Greenhorn

Joined: Sep 25, 2010
Posts: 25
If I have a name like John Wilkes Booth how would I get the last initial of each name ?
Mohamed Sanaulla
Bartender

Joined: Sep 08, 2007
Posts: 2928
    
  15

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?


Mohamed Sanaulla | My Blog
swaraj gupta
Ranch Hand

Joined: Oct 22, 2010
Posts: 181

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

Joined: Sep 25, 2010
Posts: 25
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

Joined: Sep 08, 2007
Posts: 2928
    
  15

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

Joined: Sep 25, 2010
Posts: 25
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

Joined: Sep 08, 2007
Posts: 2928
    
  15

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

Joined: Oct 22, 2010
Posts: 181

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

Joined: Mar 05, 2008
Posts: 2782
    
    2
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

Joined: Sep 25, 2010
Posts: 25
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

Joined: Mar 05, 2008
Posts: 2782
    
    2
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

Joined: Sep 25, 2010
Posts: 25
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

Joined: Oct 31, 2010
Posts: 1
Maybe something like that:

 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: extracting last initial
 
Similar Threads
ClassNotFoundException
&& operator
Boolean and boolean problems in JavaBeans
Hi all
Regarding SCJP 5