Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Cannot figure out why this is wrong............

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    4
    My Mood
    Grumpy
    Thanks
    1
    Thanked 0 Times in 0 Posts

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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Cannot figure out why this is wrong............

    "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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    gabgab96 (February 26th, 2013)

  4. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    4
    My Mood
    Grumpy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

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

  5. #4
    Junior Member
    Join Date
    Feb 2013
    Posts
    4
    My Mood
    Grumpy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Cannot figure out why this is wrong............

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #6
    Junior Member
    Join Date
    Feb 2013
    Posts
    4
    My Mood
    Grumpy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Cannot figure out why this is wrong............

    Okay. So I added a return statement:
    public String initials()
    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 View Post
    Okay. So I added a return statement:
    public String initials()
    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.

  8. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Cannot figure out why this is wrong............

    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:
    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 ^
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Simple sorting program, can't figure out whats wrong with my code
    By USC CSCE in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 29th, 2012, 08:38 AM
  2. Can't figure out what's wrong
    By xdx in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 10th, 2012, 08:48 AM
  3. My pseudocode is apparently wrong, and I can't figure out why...
    By Eclecstatic in forum What's Wrong With My Code?
    Replies: 0
    Last Post: September 12th, 2012, 12:17 AM
  4. i cannot figure out what's wrong
    By biyuss in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 17th, 2011, 09:22 PM
  5. can't figure whats wrong with my add method?? help!
    By b094mph in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 8th, 2011, 05:00 PM

Tags for this Thread