Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Use a Method in Java to find the Last name of any given name

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Use a Method in Java to find the Last name of any given name

    I need to use a method to find out the last name of any given name - meaning given any full name as a parameter the method should return the last name.

    Here is the code that I have written but which will not compile. Need help! Thanks in advance


    class LastName {
    public static void main(String[] args) {
    getlastName(" ");
    System.out.println();
    }

    public static String getlastName(String lastName) {

    System.out.println("Enter your full name: ");

    int firstSpace = fullName.indexOf(" ");
    int middleSpace = fullName.middleIndexOf(" ");
    lastName = fullName.substring(firstSpace,middleSpace);
    return lastName;
    }
    }
    .


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Use a Method in Java to find the Last name of any given name

    which will not compile
    Use code tags
    BB Code List - Java Programming Forums - The Java Community

    ... and copy-paste your compiler errors.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Use a Method in Java to find the Last name of any given name

    given any full name as a parameter the method should return the last name.
    What is the format for the full name?
    last, first
    last first
    first last
    first middle last
    If the full name is in a String and you know what format it is in, the String class has several methods that would be useful: indexOf, substring and split for example.

  4. #4
    Junior Member
    Join Date
    Feb 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Use a Method in Java to find the Last name of any given name

    The format for the full name is First, Middle, Last.

    Thanks in advance.

    Best Regards,

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Use a Method in Java to find the Last name of any given name

    Look at the different versions of the indexOf method to scan to the second or last space.

  6. #6
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Use a Method in Java to find the Last name of any given name

    getlastName(" "); is returning a String and where in your program you are trying to get the returned value?
    If it's not even compiling, copy & paste the full description of errors here.

Similar Threads

  1. Remaining compile errors: no suitable method found for & cannot find symbol
    By ChuckLep in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 12th, 2011, 03:33 PM
  2. [SOLVED] Cannot find symbol - Calling method passing array Pets
    By Rilstin81 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 11th, 2011, 03:27 PM
  3. Matcher object .find() method question
    By chronoz13 in forum Java SE APIs
    Replies: 18
    Last Post: September 8th, 2011, 11:18 AM
  4. Using Queue, cannot find symbol method enqueue
    By firebluetom in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 17th, 2011, 01:41 PM
  5. cannot find symbol - method
    By kyuss in forum Object Oriented Programming
    Replies: 2
    Last Post: December 7th, 2009, 01:01 PM