my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Create a Name class to manage different name changes of a person. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Create a Name class to manage different name changes of a person." Watch "Create a Name class to manage different name changes of a person." New topic
Author

Create a Name class to manage different name changes of a person.

Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 58
So far I have gotten this far in writing my Name class:


And I'm starting to test what I have so far. When I enter my full name though, I get:
First name: null


Can anyone help me figure out what I'm doing wrong? Thanks! Y'all are the best!

Name(): Initializes full and first names to “Anonymous” and middle, last
and maiden names to empty string.
Name(String fullName): Initializes full name to fullName and initializes
first, middle and last names appropriately. If only 2 names are in the
fullName, middle name is set to empty string. For all cases, maiden name
is set to empty string.
Keith Rainey
Ranch Hand

Joined: Jan 19, 2011
Posts: 66

It looks like you have your assignments reversed: nameSplit[0] = firstName;


rather than firstname=nameSplit[0];




Keith Rainey
OCPJP6
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

Keith Rainey wrote:It looks like you have your assignments reversed: nameSplit[0] = firstName;


rather than firstname=nameSplit[0];




Oh yeah, how can you miss that statement? Nice catch.


Mohamed Sanaulla | My Blog
Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 58
Gahh. I didn't even notice that. Thanks!!
Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 58
Ok. I've made some changes to my code. And now I'm having a problem getting the full name to print out correctly. For example: Nicolas Patric Young should print out as Nicolas P. Young


Keith Rainey
Ranch Hand

Joined: Jan 19, 2011
Posts: 66

I don't see where you actually call the getFullName() method anywhere....
Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 58
Wow. I feel really dumb. Haha, thanks for pointing that out. Now I'm almost done with everything. I'm writing my test class and am having issues compiling because of the methods and for loops. It's probably another really stupid mistake but I just can't figure it out.
If you guys could help me with this I would appreciate it so much! Y'all have helped me out so much already!



Thank you!!!>
Keith Rainey
Ranch Hand

Joined: Jan 19, 2011
Posts: 66

Kd Martin wrote: Now I'm almost done with everything. I'm writing my test class and am having issues compiling because of the methods and for loops. It's probably another really stupid mistake but I just can't figure it out. Thank you!!!>



That's a pretty vague description of the problem.....
Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 58
Oops! Sorry, I forgot to paste that. Here it is:

You will create a Name class to manage different name
changes of a person.

Test Program
1. Have the user enter the number of names (=x).
2. Have the user enter in x full names (e.g. Hillary Diane Rodham, Barrack Obama).
3. While (The user hasn’t typed in “exit” or “Exit”)
Have the user type in a person’s first name
Ask which name they would like to change (first, middle or last)
Ask the user what they would like to change that name to
If they want to change last name, ask the user if they would like to change maiden name also.
If yes, current last name becomes maiden name, and new last name becomes last name.
4. Print everybody’s name in the specified format:


Print format:
Full Name: Hillary D. Clinton (Note, only middle initial is printed)
First Name: Hillary
Middle Name: Diane
Last Name: Clinton
Maiden Name: Rodham

Keith Rainey
Ranch Hand

Joined: Jan 19, 2011
Posts: 66

I'll rephrase it a bit...

What's not working?


I think the general atmosphere here is to lend a helping hand understanding concepts.... not to do the assignment for you.

Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 58
I'm getting a bunch of compiling errors all pertaining to the methods and the for loops.
TestName.java:31: illegal start of expression
public static boolean cont(String response) {
^
TestName.java:31: illegal start of expression
public static boolean cont(String response) {
^
TestName.java:31: ';' expected
public static boolean cont(String response) {
^
TestName.java:31: ')' expected
public static boolean cont(String response) {
^
TestName.java:31: illegal start of expression
public static boolean cont(String response) {
^
TestName.java:31: ';' expected
public static boolean cont(String response) {
^
TestName.java:71: illegal start of type
for (int i=0; i<nameArray.Length; i++) {
^
TestName.java:71: ')' expected
for (int i=0; i><nameArray.Length; i++) {
^
TestName.java:71: illegal start of type
for (int i=0; i><nameArray.Length; i++) {
^
TestName.java:71: ><identifier> expected
for (int i=0; i<nameArray.Length; i++) {
^
TestName.java:71: ';' expected
for (int i=0; i><nameArray.Length; i++) {
^
TestName.java:71: > expected
for (int i=0; i<nameArray.Length; i++) {
^
TestName.java:71: '(' expected
for (int i=0; i><nameArray.Length; i++) {
^
TestName.java:71: ><identifier> expected
for (int i=0; i<nameArray.Length; i++) {
^
TestName.java:71: illegal start of type
for (int i=0; i><nameArray.Length; i++) {
^
TestName.java:71: ><identifier> expected
for (int i=0; i<nameArray.Length; i++) {
^
TestName.java:71: ';' expected
for (int i=0; i><nameArray.Length; i++) {
^
TestName.java:72: illegal start of type
nameArray[i].displayNames();
^
TestName.java:72: ';' expected
nameArray[i].displayNames();
^
TestName.java:72: invalid method declaration; return type required
nameArray[i].displayNames();
^
TestName.java:74: class, interface, or enum expected
}
^
21 errors
>
Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 58
I know its probably just a couple issues that are causing these problems, but I just can't figure out what I'm doing wrong when I create the methods in the TestName class. I'm thinking it has something to do with the array of objects but I'm just not sure what.
Keith Rainey
Ranch Hand

Joined: Jan 19, 2011
Posts: 66

Typically when you see a whole bunch of syntax related errors such as missing semi-colon, parens, etc, the series of errors spawns from one syntax error. If you start at the top of the error listing, verify matching braces and parens, properly placed semi-colons,etc. The errors give you a clue what to look for (usually) and the line number of the file where to start looking.


Start here:

You don't have an array called "names"
The size of an array is found with .length, not .Length
Keith Rainey
Ranch Hand

Joined: Jan 19, 2011
Posts: 66

You're missing a curly brace which makes the following methods look like they are inside the main method.

Define the 'response' variable outside the do-loop. (variable scoping)

Do arrays have a startsWith() method?



You'll have some other issues to sort out once you fix those. Read the error messages. They are typically useful in figuring out where to start looking for the problems.
Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 58
Fixing that for loop didn't change any of the errors :/ I think my way of searching through the array of strings is incorrect. Could that be part of the issue I'm having compiling?
Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 58
Oh! I didn't see that. Thank you
Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 58
This is what I have now


And now I only have 2 errors.

TestName.java:29: cannot find symbol
symbol : variable response
location: class TestName
} while (cont(response));
^
TestName.java:51: cannot find symbol
symbol : method startswith(java.lang.String)
location: class Name
boolean b = nameArray[i].startswith(first_);

For the do while loop, I want it to continue if the user's response is not the word "Exit". I created a boolean method for that purpose, but I don't see how it cannot find the symbol. I also don't understand how a symbol is not found for the startswith method in line 51. I thought I defined all the variables :/>
Keith Rainey
Ranch Hand

Joined: Jan 19, 2011
Posts: 66

I mentioned the problem with 'response' in my previous post.

As for 'startswith', remember that everything in Java is case sensitive.
Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 58
Ok, I fixed response, but changing startswith to startsWith didn't fix the problem :/
Keith Rainey
Ranch Hand

Joined: Jan 19, 2011
Posts: 66

I put it as a question before: does the startWith() method exist for arrays?

(if it doesn't exist, you can't call it, right?)
Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 58
I figured it out! Thank you so much for your help!
Keith Rainey
Ranch Hand

Joined: Jan 19, 2011
Posts: 66

Congratulations! That gives a nice sense of accomplishment, right?
 
 
subject: Create a Name class to manage different name changes of a person.