944,219 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 708
  • Java RSS
Nov 2nd, 2009
0

Need Initials

Expand Post »
Help! The only thing I seem to not get is the initials from the substring........... any ideas?

java Syntax (Toggle Plain Text)
  1. public class Name {
  2.  
  3. //Data Members
  4. private String firstName;
  5. private String middleName;
  6. private String lastName;
  7. private String initials;
  8.  
  9. //Default constructor
  10.  
  11. Name() {
  12.  
  13. firstName = null;
  14. middleName = null;
  15. lastName = null;
  16. }
  17.  
  18. //Constructors
  19. Name(String newFirstName, String newMiddleName, String newLastName){
  20. firstName = newFirstName;
  21. middleName = newMiddleName;
  22. lastName = newLastName;
  23. }
  24.  
  25. //Mutator methods
  26.  
  27. public void setFirstName(String newFirstName){
  28. firstName = newFirstName;
  29. }
  30. public void setMiddleName(String newMiddleName){
  31. middleName = newMiddleName;
  32. }
  33. public void setLastName(String newLastName){
  34. lastName = newLastName;
  35. }
  36.  
  37. //Accessor method
  38. public String getFirstName(){
  39. return firstName;
  40. }
  41. public String getMiddleName(){
  42. return middleName;
  43. }
  44. public String getLastName(){
  45. return lastName;
  46. }
  47.  
  48. //Returns the total number of characters in the full nmae, not including spaces
  49.  
  50. public void concat(){
  51. String fullName=firstName.concat(middleName).concat(lastName);
  52. int lengthName=fullName.length();
  53. System.out.println("The length in characters of " + firstName + " " +middleName+ " " + lastName + " is: " + lengthName);
  54. }
  55.  
  56. //Returns firstname middle last name initial with 3 character initials for example CMP
  57. public void substring(){
  58. initials = firstName.substring(0) + middleName.substring(0) + lastName.substring(0);
  59. System.out.println(initials);
  60. }
  61.  
  62.  
  63. //Returns a string containing the person's full name with last name first followed by a comma
  64. public void lastNameFirst(){
  65. String s = lastName.concat(", ").concat(firstName).concat(" "+middleName.substring(0));
  66. System.out.println(s);
  67. }
  68. public void fullNameInitials(){
  69. String f = firstName.concat(" " +middleName).concat(" "+lastName+" ") + initials;
  70. System.out.println(f);
  71. }
  72. }
  73.  
  74.  
  75. [CODE=java]public class NewNamesList {
  76. public static void main(String[] args){
  77.  
  78. Name[] namesList = new Name[2];
  79.  
  80. namesList[0] = new Name("Jessica", "Mary", "Preston");
  81. namesList[1] = new Name("Cheyenne", "Mary", "Palmore");
  82.  
  83. namesList[0].concat();
  84. namesList[0].substring();
  85. namesList[0].lastNameFirst();
  86. namesList[0].fullNameInitials();
  87.  
  88. namesList[1].concat();
  89. namesList[1].substring();
  90. namesList[1].lastNameFirst();
  91. namesList[1].fullNameInitials();
  92. }
  93. }
[/CODE]
Reputation Points: 10
Solved Threads: 0
Light Poster
JRabbit2307 is offline Offline
32 posts
since Oct 2009
Nov 2nd, 2009
0
Re: Need Initials
It seems like you have three Strings, firstName, middleName, and lastName. If that is the case, use

Java Syntax (Toggle Plain Text)
  1. 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.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Nov 2nd, 2009
0

Reply for bestjew for initials..

java Syntax (Toggle Plain Text)
  1. //Returns firstname middle last name initial with 3 character initials for example CMP
  2. public char charAt(int index){
  3. String initials = firstName.charAt(0) + middleName.charAt(0) + lastName.charAt(0);
  4. System.out.println(intials);
  5. }

i'm getting an error of incompatible types for Line 3
Reputation Points: 10
Solved Threads: 0
Light Poster
JRabbit2307 is offline Offline
32 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Java Menu
Next Thread in Java Forum Timeline: hey i need some help looking over my coding





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC