I'm trying to code a program that returns the initials of a name and its length. However, in line 210 it gives me a "missing return statement" the code is below.
import java.io.*;
public class Name
{
/*
* Instance variables, these are marked private
* so that they can't be seen or directly changed
* by classes that use the Address class. They
* will not show up in the generated documentation.
*
* The only way other classes can change these is by
* using the public methods contained in Address,
* This is an example of encapsulation (information hiding).
*/
private String firstName;
private String middleName;
private String lastName;
private String initials;
/**
* This constructor has no parameters, it contructs
* an Address object with all of the fields initialized
* with null values. Instance variables are automatically
* initialized in Java but it is best to explicitly
* initialize them for readability purposes. This
* constructor overrides the default constructor
* which has no parameters.
*/
public Name()
{
// initialize instance variables
firstName = null;
middleName = null;
lastName = null;
}// end of Address() constructor
/**
* This constructor creates an object of type Address.
* It takes 6 parameters and initializes the instance
* variables with them.
*/
public Name(String fName, String mName, String lName)
{
// initialize instance variables, the parameter names are different,
// you could use the same names but you would have to address them
// as follows: this.firstName = firstName;
// <u>this</u> refers to the object itself and indicates that the parameter
// value goes in the object's instance variable
firstName = fName;
middleName = mName;
lastName = lName;
}// end of Address(String fName, String lName, String sAddress,
// String city, String state, String zip) constructor
/**
* This method is the mutator method for the firstName.
* The method allows the firstName instance variable to
* be changed.
*
* @param fName a String for holding the first name
*/
public void setFirstName(String fName)
{
firstName = fName;
}// end of setFirstName(String fName)
/**
* This method is the mutator method for the lastName.
* The method allows the lastName instance variable to
* be changed.
*
* @param lName a String for holding the last name
*/
public void setMiddleName(String mName)
{
middleName = mName;
}// end of setLastName(String lName)
/**
* This method is the mutator method for the streetAddress.
* The method allows the streetAddress instance variable to
* be changed.
*
* @param sAddress a String for holding the street address
*/
public void setLastName(String lName)
{
lastName = lName;
}// end of setStreetAddress(String sAddress)
/**
* This method is the mutator method for the cityName.
* The method allows the cityName instance variable to
* be changed.
*
* @param city a String for holding the city name
*/
/**
* This method is the accessor method for the firstName.
* The method allows the firstName instance variable to
* be accessed or retrieved.
*
* @return firstName a String representing a first name
*/
public String getFirstName()
{
return firstName;
}// end of getFirstName()
/**
* This method is the accessor method for the lastName.
* The method allows the lastName instance variable to
* be accessed or retrieved.
*
* @return lastName a String representing a last name
*/
public String getMiddleName()
{
return middleName;
}// end of getLastName()
/**
* This method is the accessor method for the streetAddress.
* The method allows the streetAddress instance variable to
* be accessed or retrieved.
*
* @return streetAddress a String representing the street address
*/
public String getLastName()
{
return lastName;
}// end of getStreetAddress()
/**
* This method is the accessor method to determine
* if all of the fields in the record are filled in.
*
* @return complete a boolean indicating if the record is complete
*/
public boolean isCompleteRecord()
{
boolean complete = true;
if(firstName == null) {
complete = false;
} else if(middleName == null) {
complete = false;
} else if(lastName == null) {
complete = false;
}
return complete;
}// end of isCompleteRecord()
/**
* This method is the equals method to determine
* if two Address objects are the same.
*
* @return equal as true if the two objects are equal
*/
public boolean equals(Object otherName)
{
boolean equal = false;
if(this.getFirstName().equals(((Name)otherName).ge tFirstName()))
if(this.getMiddleName().equals(((Name)otherName).g etMiddleName()))
if(this.getLastName().equals(((Name)otherName).get LastName()))
{
equal = true;
}
return equal;
}// end of equals()
/**
* This method returns a String containing the address
*
* @return firstName a String containing the complete address
*/
public String toString()
{
return getFirstName()+" "+getMiddleName()+" "+getLastName();
}// end of toString()
public String toLast()
{
return getLastName()+" "+getFirstName()+" "+getMiddleName();
}
public String initials()
{
firstName.charAt(0);
middleName.charAt (0);
lastName.charAt (0);
}
public int length()
{
return firstName.length();
}
/**
* This method outputs the address to the console
*/
public void toString(boolean print)
{
if(print)
{
System.out.print(getFirstName());
System.out.print(" ");
System.out.print(getMiddleName());
System.out.println();
System.out.print(getLastName());
System.out.println();
}
}// end of toString(boolean print)
}
the TestName is over here:
public class TestName {
public static void main(String[] args) throws Exception {
// Create a File instance
java.io.File file = new java.io.File("names.txt");
java.util.Scanner input = new java.util.Scanner(file);
Name[] nameArray = new Name[10];
System.out.println(input.delimiter());
// Read data from a file
int index = 0;
while (input.hasNext()) {
String first = input.next();
String middle = input.next();
String last = input.next();
Name name = new Name(first, middle, last);
nameArray[index++] = name;
}
printNameArray(nameArray);
// Close the file
input.close();
}
public static void printNameArray(Name[] nameArray) {
Name name;
String first;
String middle;
String last;
for (int i = 0; i < nameArray.length; i++) {
name = nameArray[i];
first = name.getFirst();
middle = name.getMiddle();
last = name.getLast();
System.out.println(last + ", " + first + " " + middle + "\n");
}
}
}