Results 1 to 5 of 5
Like Tree3Likes
  • 1 Post By mwr1976
  • 1 Post By JosAH
  • 1 Post By tnrh1

Thread: charAt problem

  1. #1
    tangel is offline Member
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Default 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;
    }

  2. #2
    mwr1976 is offline Senior Member
    Join Date
    Oct 2011
    Posts
    106
    Rep Power
    0

    Default 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.
    tangel likes this.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,044
    Blog Entries
    7
    Rep Power
    23

    Default 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,

    Jos
    tangel likes this.
    The only person who got everything done by Friday was Robinson Crusoe.

  4. #4
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    5

    Default 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.
    tangel likes this.

  5. #5
    tangel is offline Member
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Default Re: charAt problem

    I was unsure of what you meant by API documentation until tnrh1 posted the link. It's super helpful! I'm a newbie at Java so thank you very much! It works now.

Similar Threads

  1. Str.charAt() ?!
    By HearT.Hunt3r in forum New To Java
    Replies: 21
    Last Post: 08-23-2011, 07:42 AM
  2. Why won't charAt() work?
    By MetalR0 in forum New To Java
    Replies: 6
    Last Post: 08-04-2011, 09:46 AM
  3. charAt problem... Please help
    By Matija in forum New To Java
    Replies: 7
    Last Post: 01-21-2011, 02:20 AM
  4. Space for charAt()?
    By Tussmann in forum New To Java
    Replies: 5
    Last Post: 11-02-2010, 07:57 PM
  5. Help with charAt()
    By HackerOfDoom in forum New To Java
    Replies: 7
    Last Post: 03-21-2010, 06:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •