Results 1 to 16 of 16
  1. #1
    mikeitalydz is offline Member
    Join Date
    Apr 2009
    Posts
    18
    Rep Power
    0

    Default how do i initials the word so i get the first letter only

    First Thread in Java
    Hello every one Iím new to Java and my question here is how I canít get the initials of any word such the name John i want to get only the letter "J" will be displayed I tried to use split function but didnít help and this is my code :
    String fname;

    String mname;
    String lname;




    fname = JOptionPane.showInputDialog(alert, "What is Your First Name: ");
    mname = JOptionPane.showInputDialog(alert, "What Is your Middle Name: ");
    lname = JOptionPane.showInputDialog(alert, "What Is your Last Name: ");
    JOptionPane.showMessageDialog(alert, "The full name is : " +fname.substring(0)." "+mname.substring(0)+ " "+lname.substring(0)+ " . ");

    after I got the full name like John Bob, Mike
    I want to initials the names so i get the first letter of each word the output will be J B M
    I need your help
    thank you

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You might want to have a look at the String API documentation: in particular what the substring() method does and for other methods that are available to extract a particular character.

    If you get stuck, post compilable code (or if it doesn't compile, full compiler messages). Rather than say something "doesn't work", say what it does do and what you were expecting or intending it would do.

  3. #3
    mikeitalydz is offline Member
    Join Date
    Apr 2009
    Posts
    18
    Rep Power
    0

    Default

    Thank you for your reply and the error is : the full name is [Ljava.lang.String;@84ce7a [Ljava.lang.String;@12b6c89 [Ljava.lang.String;@1e2befa.

  4. #4
    mikeitalydz is offline Member
    Join Date
    Apr 2009
    Posts
    18
    Rep Power
    0

    Default and the code again after i used split functin it looks like this

    fname = JOptionPane.showInputDialog(alert, "What is Your First Name: ");
    mname = JOptionPane.showInputDialog(alert, "What Is your Middle Name: ");
    lname = JOptionPane.showInputDialog(alert, "What Is your Last Name: ");
    JOptionPane.showMessageDialog(alert, "The full name is : " +fname.split(fname)+ " "+mname.split(mname)+ " "+lname.split(lname)+ " . ");

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I think you may have misunderstood the point about compilable code: the point is to post code that others could run to see the problem for themselves.

    Anyway, look at the part of the output that says: ""fname.split(fname)". The idea was to obtain just the first letter of the user's first name. But that is not what the split() method does - or anything like it. Did you read through the String documentation? Because there really is no substitute for that: the split() method is going to do whatever it is documented to do. Wishful thinking counts for nothing when writing a computer program.

    If you haven't read that documentation, do so now. If you have, read it again because there is a method in the String class that lets you extract a character At the start of a string.

  6. #6
    mikeitalydz is offline Member
    Join Date
    Apr 2009
    Posts
    18
    Rep Power
    0

    Default

    thank you .

  7. #7
    mikeitalydz is offline Member
    Join Date
    Apr 2009
    Posts
    18
    Rep Power
    0

    Default

    this is the code :
    import javax.swing.*;


    public class AlertBox

    {
    public static void main (String [] args)
    {

    String fname;
    String mname;
    String lname;

    fname = JOptionPane.showInputDialog(null, "What is Your First Name: ");
    mname = JOptionPane.showInputDialog(null, "What Is your Middle Name: ");
    lname = JOptionPane.showInputDialog(null, "What Is your Last Name: ");
    JOptionPane.showMessageDialog(null, "The full name is : " +fname.split(fname)+ " "+mname.split(mname)+ " "+lname.split(lname)+ " . ");


    }}

  8. #8
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    are you aware of what charAt() does?

  9. #9
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    We've seen this simple homework assignment before, from others who likewise can't be bothered doing their own work. If you want to pass your course you have to put in some effort, otherwise you deserve to fail. This is how life works.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  10. #10
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    Yes ... pay attention to emceenugget's suggeston. You could use the split() method (and you're using it wrong anyway) if you received the name in one string:
    Java Code:
    String fullName = "John Henry Smith";
    
    String[] nameArray = fullName.split (" ");
    Each element in the array would be a name.

    But since you have the names as separate strings, the charAt() method would be yur best bet.

    Luck,
    CJSL
    Last edited by CJSLMAN; 04-18-2009 at 01:53 AM. Reason: Correct a typo :-)
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  11. #11
    mikeitalydz is offline Member
    Join Date
    Apr 2009
    Posts
    18
    Rep Power
    0

    Default

    by the way i do not like to give my homework to any one and if you read my message you do understand why i asked that kind of question sorry MR Java Man
    i said i am new to java and to programming im learing on my own but thanks for your advice, this is not the way how to giving people advices you are so negative you should learn how to speak but again thanks alot for Chris and Emceenugget

  12. #12
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    I was just pointing out that this exact question has been posted before. If you'd searched on your own first you would have found the answer.

    Reposting the same code three times without showing signs of having followed people's suggestions is a sign of the typical lazy student we get around here. Apologies if I have misbranded you.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  13. #13
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    hhhmm... interesting exchange of opinions... anyway, mikeitalydz, did you understand how to use the string charAt() method?

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  14. #14
    mikeitalydz is offline Member
    Join Date
    Apr 2009
    Posts
    18
    Rep Power
    0

    Default hi Chris , Thanks alot i did read it and i did understand how it works

    public static void main (String [] args)
    {

    String fname;
    String mname;
    String lname;

    fname = JOptionPane.showInputDialog(null, "What is Your First Name: ");
    mname = JOptionPane.showInputDialog(null, "What Is your Middle Name: ");
    lname = JOptionPane.showInputDialog(null, "What Is your Last Name: ");
    JOptionPane.showMessageDialog(null, "The full name is : " +fname.charAt(0)+ " "+mname.charAt(0)+ " "+lname.charAt(0)+ " . ");

  15. #15
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    Great !!! Problem solved?

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  16. #16
    mikeitalydz is offline Member
    Join Date
    Apr 2009
    Posts
    18
    Rep Power
    0

Similar Threads

  1. Printing Out Every Other Letter
    By Agent in forum New To Java
    Replies: 4
    Last Post: 11-20-2008, 12:43 AM
  2. Letter with Letters
    By elgatoboricua in forum New To Java
    Replies: 7
    Last Post: 09-16-2008, 02:59 PM
  3. Replies: 0
    Last Post: 06-25-2008, 10:34 AM
  4. Replies: 20
    Last Post: 05-14-2008, 09:42 AM
  5. Drive letter sniff/open explorer window
    By iiz in forum New To Java
    Replies: 1
    Last Post: 04-04-2008, 03:50 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
  •