Results 1 to 5 of 5
Thread: charAt problem
- 10-26-2011, 07:26 AM #1
Member
- Join Date
- Oct 2011
- Posts
- 4
- Rep Power
- 0
charAt problem
The point is that if i feed it a name with a first and last name, it should find the space and return everything after the space... but i keep getting an error Dr. Java. Am i using charAt correctly?
If somebody could help me with this it would be kindly noted. Thank you!
here is the code:
public static String getLastName(String author)
{int space =author.charAt(" ");String lastName=author.substring(space+1);return lastName;}
- 10-26-2011, 07:32 AM #2
Senior Member
- Join Date
- Oct 2011
- Posts
- 106
- Rep Power
- 0
Re: charAt problem
no you are not using the method correctly. The parameter for the charAt() method is an int not a char. It takes the index and returns a char.
- 10-26-2011, 07:38 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,398
- Blog Entries
- 7
- Rep Power
- 17
Re: charAt problem
I think you want to use the indexOf( ... ) method; read the API documentation for the String class and you know everything.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-26-2011, 04:32 PM #4
Senior Member
- Join Date
- Aug 2011
- Posts
- 248
- Rep Power
- 2
Re: charAt problem
The CharAt method returns the char at the index you mention.
For exmaple:
st = "abcd";
st.CharAt(2) equals to "c" .
As josAH said, I think you mean IndexOf, anyway you still can do it with CharAt:
run a loop on the whole string checking each char if it equals to " " .
if found return the index of the next char (last name start). // "john smith" that should return 5 (make sure you count from 0!).
else return -1
Then check the returned value,
if >-1
cut the string from the returned value till the end (st.length()) you might ask why not length()-1? the SubString method cuts from x to y-1.
Check this page to get more information: String (Java 2 Platform SE v1.4.2)Last edited by tnrh1; 10-26-2011 at 04:36 PM.
- 10-26-2011, 04:49 PM #5
Member
- Join Date
- Oct 2011
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Str.charAt() ?!
By HearT.Hunt3r in forum New To JavaReplies: 21Last Post: 08-23-2011, 07:42 AM -
Why won't charAt() work?
By MetalR0 in forum New To JavaReplies: 6Last Post: 08-04-2011, 09:46 AM -
charAt problem... Please help
By Matija in forum New To JavaReplies: 7Last Post: 01-21-2011, 01:20 AM -
Space for charAt()?
By Tussmann in forum New To JavaReplies: 5Last Post: 11-02-2010, 06:57 PM -
Help with charAt()
By HackerOfDoom in forum New To JavaReplies: 7Last Post: 03-21-2010, 05:27 PM


3Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks