Results 1 to 4 of 4
  1. #1
    Gagnrath is offline Member
    Join Date
    Jun 2013
    Posts
    9
    Rep Power
    0

    Default Getting first initial and last name from full name

    So I have been trying to figure out how to create an employee ID for a simple program I am working on and I got most of the code to get the first name and last name. I can make it display the full name, but the type of ID want to go for is the first initial and last name.

    ex. Tyler Durden to TDurden

    Here is what I have for the code so far:

    Java Code:
      void newEmp()
        {
            String firstName;
            String lastName;
            String listName = new String();
            Scanner emp = new Scanner(System.in);
            System.out.println("\t\t New Employee Profile Generator\n\n");
            System.out.print("First Name: ");
            firstName = emp.nextLine();
            System.out.print("Last Name: ");
            lastName = emp.nextLine();
            listName = firstName + " " + lastName;
            System.out.println();
           /* String empId =emp.nextLine();
            firstName = empId.substring(0);
            String firstInitial = firstName.substring(0);
            String employeeId = firstInitial + lastName;*/
            System.out.println("Thank You " + listName);
            System.out.println("Your new new Employee ID is "/* + employeeId*/);
        }
    the items between the /* */ are attempts at trying to get it to work.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: Getting first initial and last name from full name

    Well basically you need only the first character of the name, right? Isn't there a method in the String class that is more fitting to that requirement than substring()?

    String (Java Platform SE 7 )


    Note: as an exercise, you should read the API documentation of the String substring() methods (plural) and make it work using that method too.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    Gagnrath is offline Member
    Join Date
    Jun 2013
    Posts
    9
    Rep Power
    0

    Default Re: Getting first initial and last name from full name

    After some digging, I finally found an article that explained it pretty well.

    Manipulating Characters in a String (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)

    Here is the Code that I needed to use

    Java Code:
        {
            String firstName;
            String lastName;
            String listName = new String();
            Scanner emp = new Scanner(System.in);
            System.out.println("\t\t New Employee Profile Generator\n\n");
            System.out.print("First Name: ");
            firstName = emp.nextLine();
            System.out.print("Last Name: ");
            lastName = emp.nextLine();
            listName = firstName + " " + lastName;
            System.out.println();
            String empId = firstName;
            char empChar = empId.charAt(0);
            String employeeId = empChar + lastName;
            System.out.println("Thank You " + listName);
            System.out.println("Your new new Employee ID is " + employeeId);
        }

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: Getting first initial and last name from full name

    The API documentation I linked to should have been enough, but I'm glad you found a way through your own research at least.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 3
    Last Post: 10-10-2012, 08:34 PM
  2. How to substract each value from the initial value.
    By whateverme in forum New To Java
    Replies: 8
    Last Post: 10-27-2010, 03:26 PM
  3. tricky initial value problem
    By Black_Eye in forum New To Java
    Replies: 4
    Last Post: 10-19-2010, 10:02 AM
  4. Initial capacity of ArrayList
    By bugger in forum New To Java
    Replies: 12
    Last Post: 12-30-2009, 09:16 AM
  5. Replies: 1
    Last Post: 08-13-2007, 04:46 AM

Posting Permissions

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