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

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);
   }
}


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();
    }
}

Recommended Answers

All 2 Replies

It seems like you have three Strings, firstName, middleName, and lastName. If that is the case, use

String initials = firstName.charAt(0) + middleName.charAt(0) + lastName.charAt(0);

instead of the substring method you're using.

//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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.