Results 1 to 16 of 16
- 04-17-2009, 11:35 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 18
- Rep Power
- 0
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
- 04-17-2009, 11:47 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 04-18-2009, 12:05 AM #3
Member
- Join Date
- Apr 2009
- Posts
- 18
- Rep Power
- 0
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.
- 04-18-2009, 12:08 AM #4
Member
- Join Date
- Apr 2009
- Posts
- 18
- Rep Power
- 0
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)+ " . ");
- 04-18-2009, 12:22 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 04-18-2009, 12:36 AM #6
Member
- Join Date
- Apr 2009
- Posts
- 18
- Rep Power
- 0
thank you .
- 04-18-2009, 12:39 AM #7
Member
- Join Date
- Apr 2009
- Posts
- 18
- Rep Power
- 0
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)+ " . ");
}}
- 04-18-2009, 12:51 AM #8
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
are you aware of what charAt() does?
- 04-18-2009, 12:57 AM #9
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
- 04-18-2009, 01:03 AM #10
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:
Each element in the array would be a name.Java Code:String fullName = "John Henry Smith"; String[] nameArray = fullName.split (" ");
But since you have the names as separate strings, the charAt() method would be yur best bet.
Luck,
CJSLLast 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.
- 04-18-2009, 01:31 AM #11
Member
- Join Date
- Apr 2009
- Posts
- 18
- Rep Power
- 0
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
- 04-18-2009, 01:55 AM #12
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
- 04-18-2009, 02:00 AM #13
hhhmm... interesting exchange of opinions... anyway, mikeitalydz, did you understand how to use the string charAt() method?
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 04-18-2009, 04:30 AM #14
Member
- Join Date
- Apr 2009
- Posts
- 18
- Rep Power
- 0
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)+ " . ");
- 04-18-2009, 05:02 AM #15
Great !!! Problem solved?
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 04-19-2009, 10:35 AM #16
Member
- Join Date
- Apr 2009
- Posts
- 18
- Rep Power
- 0
Similar Threads
-
Printing Out Every Other Letter
By Agent in forum New To JavaReplies: 4Last Post: 11-20-2008, 12:43 AM -
Letter with Letters
By elgatoboricua in forum New To JavaReplies: 7Last Post: 09-16-2008, 02:59 PM -
How to use TextHitInfo class to find the letter you are clicking
By Java Tip in forum java.awtReplies: 0Last Post: 06-25-2008, 10:34 AM -
the number of occurrences of each alphabetical letter in the string.
By masaka in forum New To JavaReplies: 20Last Post: 05-14-2008, 09:42 AM -
Drive letter sniff/open explorer window
By iiz in forum New To JavaReplies: 1Last Post: 04-04-2008, 03:50 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks