Need Initials

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2009
Posts: 32
Reputation: JRabbit2307 is an unknown quantity at this point 
Solved Threads: 0
JRabbit2307 JRabbit2307 is offline Offline
Light Poster

Need Initials

 
0
  #1
Nov 2nd, 2009
Help! The only thing I seem to not get is the initials from the substring........... any ideas?

  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]
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,658
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 206
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso
 
0
  #2
Nov 2nd, 2009
It seems like you have three Strings, firstName, middleName, and lastName. If that is the case, use

  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.
Out.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 32
Reputation: JRabbit2307 is an unknown quantity at this point 
Solved Threads: 0
JRabbit2307 JRabbit2307 is offline Offline
Light Poster

Reply for bestjew for initials..

 
0
  #3
Nov 2nd, 2009
  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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum


Views: 216 | Replies: 2
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC