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 2 of 2

Thread: Need basic String help

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Need basic String help

    This is basic stuff, but I'm stuck, and inexperienced. I'm working w/ Scanner and I made a string to scan the full name of a user.

    import java.util.Scanner;
     
    public class ThreeNamesNextLine
    {
    	public static void main (String [] args)
    	{
    	Scanner scan = new Scanner(System.in);
     
    	System.out.print("Please enter your name: ");
    	String fullName = scan.nextLine();

    I need to break the string down to acquire the first initials of the user when he/she enters their first, middle, and last names (there is a space between each name). So far this is what I have, which gives me the first and middle initials, how do I get the last?

            String middleInitial;
    	String lastInitial;
    	String firstLetter = fullName.substring(0,1); // for the first initial
            String lookFor = " ";
    	int space1; // indicates first space after first name is entered
     
    	space1 = fullName.indexOf( lookFor );
    	middleInitial = fullName.substring(space1 +1);
     
    	String realMiddleInitial; // don't know how to shortcut this so I had to make a new String
    	realMiddleInitial = middleInitial.substring(0,1);
     
            firstLetter = firstLetter.toUpperCase();
    	realMiddleInitial = realMiddleInitial.toUpperCase();

    It's ugly yeah, but yet again, I'm new. Thanks in advance for the help.
    Last edited by Sadalmelik; September 17th, 2011 at 11:58 PM.


  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: Need basic String help

    Unless you use java.util.regex.Pattern, this is what you'd have to do. You could possibly also try java.lang.String.split(). If you want to go with what you've got, what you need to do is to restart your search for a name-breaking character (space? dot?) from the point you last found one. There's another method called indexOf in java.lang.String which allows you to start indexOf from a specified point in the string. You saved the last position of a space in 'space1'. I reckon you'll work the rest out for yourself!

  3. The Following User Says Thank You to Sean4u For This Useful Post:

    Sadalmelik (September 18th, 2011)

Similar Threads

  1. [SOLVED] difference between String Concatenation and String -Buffer/Builder .append(<value>)
    By chronoz13 in forum Java Theory & Questions
    Replies: 5
    Last Post: September 3rd, 2011, 08:16 AM
  2. Replies: 18
    Last Post: March 2nd, 2011, 10:52 AM
  3. [SOLVED] String Matcher finding only char not a whole string
    By Kakashi in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 18th, 2011, 09:58 AM
  4. Basic Calculator
    By Yes. in forum What's Wrong With My Code?
    Replies: 13
    Last Post: June 4th, 2010, 04:24 PM
  5. Some basic questions.
    By trips in forum Java Theory & Questions
    Replies: 5
    Last Post: July 21st, 2009, 02:15 AM