| | |
Need Initials
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 32
Reputation:
Solved Threads: 0
Help! The only thing I seem to not get is the initials from the substring........... any ideas?
[/CODE]
java Syntax (Toggle Plain Text)
public class Name { //Data Members private String firstName; private String middleName; private String lastName; private String initials; //Default constructor Name() { firstName = null; middleName = null; lastName = null; } //Constructors Name(String newFirstName, String newMiddleName, String newLastName){ firstName = newFirstName; middleName = newMiddleName; lastName = newLastName; } //Mutator methods public void setFirstName(String newFirstName){ firstName = newFirstName; } public void setMiddleName(String newMiddleName){ middleName = newMiddleName; } public void setLastName(String newLastName){ lastName = newLastName; } //Accessor method public String getFirstName(){ return firstName; } public String getMiddleName(){ return middleName; } public String getLastName(){ return lastName; } //Returns the total number of characters in the full nmae, not including spaces public void concat(){ String fullName=firstName.concat(middleName).concat(lastName); int lengthName=fullName.length(); System.out.println("The length in characters of " + firstName + " " +middleName+ " " + lastName + " is: " + lengthName); } //Returns firstname middle last name initial with 3 character initials for example CMP public void substring(){ initials = firstName.substring(0) + middleName.substring(0) + lastName.substring(0); System.out.println(initials); } //Returns a string containing the person's full name with last name first followed by a comma public void lastNameFirst(){ String s = lastName.concat(", ").concat(firstName).concat(" "+middleName.substring(0)); System.out.println(s); } public void fullNameInitials(){ String f = firstName.concat(" " +middleName).concat(" "+lastName+" ") + initials; System.out.println(f); } } [CODE=java]public class NewNamesList { public static void main(String[] args){ Name[] namesList = new Name[2]; namesList[0] = new Name("Jessica", "Mary", "Preston"); namesList[1] = new Name("Cheyenne", "Mary", "Palmore"); namesList[0].concat(); namesList[0].substring(); namesList[0].lastNameFirst(); namesList[0].fullNameInitials(); namesList[1].concat(); namesList[1].substring(); namesList[1].lastNameFirst(); namesList[1].fullNameInitials(); } }
•
•
Join Date: Sep 2008
Posts: 1,658
Reputation:
Solved Threads: 206
0
#2 Nov 2nd, 2009
It seems like you have three Strings, firstName, middleName, and lastName. If that is the case, use
instead of the substring method you're using.
Java Syntax (Toggle Plain Text)
String initials = firstName.charAt(0) + middleName.charAt(0) + lastName.charAt(0);
instead of the substring method you're using.
Last edited by BestJewSinceJC; Nov 2nd, 2009 at 1:45 am.
Out.
•
•
Join Date: Oct 2009
Posts: 32
Reputation:
Solved Threads: 0
java Syntax (Toggle Plain Text)
//Returns firstname middle last name initial with 3 character initials for example CMP public char charAt(int index){ String initials = firstName.charAt(0) + middleName.charAt(0) + lastName.charAt(0); System.out.println(intials); }
i'm getting an error of incompatible types for Line 3
![]() |
Other Threads in the Java Forum
- Previous Thread: Java Menu
- Next Thread: hey i need some help looking over my coding
Views: 217 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for Java
3d @param affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth byte c# chat class classes click client code compare component corrupted database detection draw eclipse error event exception file fractal game givemetehcodez graphics gui guitesting helpwithhomework html ide image input integer j2me java java.xls javaprojects jmf jni jpanel julia keytool linux list loop map method methods mobile netbeans newbie number object oracle os pong print problem producer program programming project projectideas read recursion reflection replaysolutions rim scanner screen server set size sms socket sort sql string swing terminal test threads time transfer tree web windows






