Cannot figure out why this is wrong............
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");
}
}
}
Re: Cannot figure out why this is wrong............
Quote:
"missing return statement"
The compiler requires that all possible exits from a method that returns a value have a return statement.
Add a return statement to make the compiler happy.
Please edit your post and wrap your code with
[code=java]
<YOUR CODE HERE>
[/code]
to get highlighting and preserve formatting.
Can't figure out why this is wrong
I'm trying to figure out why this program won't work. The requirements are that I am able to grab the initials from a full name and also the length. For the life of me I can't figure out why this program doesn't work.
Code Java:
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).getFirstName()))
if(this.getMiddleName().equals(((Name)otherName).getMiddleName()))
if(this.getLastName().equals(((Name)otherName).getLastName()))
{
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 Test Program is over here:
Code Java:
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");
}
}
}
Re: Cannot figure out why this is wrong............
Yep. I put down another thread with the correct formatting. Sorry I'm new here. And I've only learned very basic java.
Re: Cannot figure out why this is wrong............
Quote:
I can't figure out why this program doesn't work.
Please explain.
If you are getting error messages, copy the full text and paste it here.
Re: Cannot figure out why this is wrong............
Okay. So I added a return statement:
public String initials()
Code java:
public String initials()
{
Char s = "firstName.charAt(0)" + "middleName.charAt (0)" + "lastName.charAt (0)";
return s;
}
error: cannot find symbol
Location: Line 206 (or the line with Char s = "firstName.charAt.......)
thanks again
--- Update ---
Quote:
Originally Posted by
gabgab96
Okay. So I added a return statement:
public String initials()
Code java:
public String initials()
{
Char s = "firstName.charAt(0)" + "middleName.charAt (0)" + "lastName.charAt (0)";
return s;
}
error: cannot find symbol
Location: Line 206 (or the line with Char s = "firstName.charAt.......)
thanks again
I added a return statement because the compiler said I needed one, FYI.
Re: Cannot figure out why this is wrong............
Quote:
error: cannot find symbol
what was the symbol that the compiler could not find?
When posting compiler error messages, you need to copy the full text of the message and paste it here.
Here is a sample:
Code :
TestSorts.java:138: cannot find symbol
symbol : variable var
location: class TestSorts
var = 2;
^
The symbol that wasn't found is pointed at by the ^